tags: Weekly Contest

Weekly Contest 406

3216. Lexicographically Smallest String After a Swap (Easy)

限制 :

  • 2 <= s.length <= 100
  • s consists only of digits.

時間複雜度:
O(n)

空間複雜度:
O(1)

程式碼:

class Solution { public: string getSmallestString(string s) { for (int i = 0; i < s.size() - 1; i++) { if (s[i] % 2 == s[i + 1] % 2 && s[i] > s[i + 1]) { swap(s[i], s[i + 1]); break; } } return s; } };

3217. Delete Nodes From Linked List Present in Array (Medium)

限制 :

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 105
  • All elements in nums are unique.
  • The number of nodes in the given list is in the range [1, 105].
  • 1 <= Node.val <=105
  • The input is generated such that there is at least one node in the linked list that has a value not present in nums.

Solution

時間複雜度:
O()

空間複雜度:
O()

程式碼:

class Solution { public: ListNode* modifiedList(vector<int>& nums, ListNode* head) { unordered_set<int> numss(nums.begin(), nums.end()); while (numss.find(head-> val) != numss.end()) { head = head->next; } ListNode* left = head; while (left && left->next != nullptr) { ListNode* right = left->next; while (right && numss.find(right->val) != numss.end()) { left->next = right->next; right = right->next; } left = right; } return head; } };

3(Medium)

限制 :

  • 104

時間複雜度:
O()

空間複雜度:
O()

程式碼:

4(Hard)

限制 :

  • 104

時間複雜度:
O()

空間複雜度:
O()

程式碼: