Posted onEdited onInLeetCodeDisqus: Symbols count in article: 562Reading time ≈1 mins.
O(n)
1 2 3 4 5 6 7 8
classSolution: defremoveElement(self, nums: List[int], val: int) -> int: i = 0 for x in nums: if x != val: nums[i] = x i += 1 return i
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { public: intremoveElement(vector<int>& nums, int val){ int i = 0; for (int x : nums) { if (x != val) { nums[i++] = x; } } return i; } };
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: intremoveElement(vector<int>& nums, int val){ int n = nums.size(); for (int i = n - 1; i >= 0; --i) { if (nums[i] == val) { swap(nums[i], nums[--n]); nums.pop_back(); } } return nums.size(); } };
Posted onEdited onInLeetCodeDisqus: Symbols count in article: 1.7kReading time ≈2 mins.
O(n) time
1 2 3 4 5 6 7 8
classSolution: defremoveDuplicates(self, nums: List[int]) -> int: i = 0 for x in nums: if nums[i] != x: i += 1 nums[i] = x return i + 1
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: intremoveDuplicates(vector<int>& nums){ if (empty(nums)) return0; int i = 0; for (int x : nums) { if (nums[i] != x) { nums[++i] = x; } } return i + 1; } };
用这个
1 2 3 4 5 6 7 8
classSolution: defremoveDuplicates(self, nums: List[int]) -> int: i = 0 for x in nums: if i < 1or nums[i - 1] < x: nums[i] = x i += 1 return i
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { public: intremoveDuplicates(vector<int>& nums){ int i = 0; for (int x : nums) { if (i < 1 || x > nums[i - 1]) { nums[i++] = x; } } return i; } };
classSolution { public: intremoveDuplicates(vector<int>& nums){ if (nums.empty()) return0; int n = nums.size(); int last = 0; for (int i = 0; i < n; ++i) { if (nums[last] != nums[i]) { nums[++last] = nums[i]; // 先加加再写就可以了 } } return last + 1; // 因为last是下标,所以要返回last + 1才是个数 } };
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next classSolution: defmergeKLists(self, lists: List[ListNode]) -> ListNode: ifnot lists: returnNone defmerge(a, b): tail = dummy = ListNode() while a and b: if a.val < b.val: tail.next = a a = a.next else: tail.next = b b = b.next tail = tail.next tail.next = a if a else b return dummy.next step, n = 1, len(lists) while step < n: for i inrange(0, n, step << 1): if i + step >= n: break lists[i] = merge(lists[i], lists[i + step]) step <<= 1 return lists[0]
Posted onEdited onInLeetCodeDisqus: Symbols count in article: 1.2kReading time ≈1 mins.
stack O(n) time O(n) space
1 2 3 4 5 6 7 8 9 10 11 12 13 14
d = {')': '(', '}': '{', ']': '['}
classSolution: defisValid(self, s: str) -> bool: stk = [] for c in s: if c in d: ifnot stk or d[c] != stk[-1]: returnFalse else: stk.pop() else: stk += c # 注意这里c是一个字符所以可以用+= returnnot stk
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
unordered_map<char, char> m = {{')', '('}, {'}', '{'}, {']', '['}}; classSolution { public: boolisValid(string s){ string stk; for (char c : s) { if (m.count(c)) { if (!stk.empty() && stk.back() == m[c]) { stk.pop_back(); } elsereturnfalse; } else { stk += c; } } return stk.empty(); } };
classSolution: deffourSum(self, nums: List[int], target: int) -> List[List[int]]: ifnot nums: return [] nums.sort() n, res = len(nums), [] # mx = nums[-1] for i inrange(n): # if nums[i] + 3 * mx < target: continue # if 4 * nums[i] > target: break if i > 0and nums[i - 1] == nums[i]: continue for j inrange(i + 1, n): # if nums[i] + nums[j] + 2 * mx < target: continue # if nums[i] + 3 * nums[j] > target: break if j > i + 1and nums[j - 1] == nums[j]: continue l, r, t = j + 1, n - 1, target - nums[i] - nums[j] while l < r: s = nums[l] + nums[r] if s == t: res.append([nums[i], nums[j], nums[l], nums[r]]) l += 1 while l < r and nums[l - 1] == nums[l]: l += 1 r -= 1 while l < r and nums[r] == nums[r + 1]: r -= 1 elif s < t: l += 1 while l < r and nums[l - 1] == nums[l]: l += 1 else: r -= 1 while l < r and nums[r] == nums[r + 1]: r -= 1 return res
classSolution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { int n = nums.size(); if (n < 4) return {}; vector<vector<int>> res; sort(nums.begin(), nums.end()); for (int i = 0; i < n; ++i) { if (i > 0 && nums[i] == nums[i - 1]) continue; // 注意跳过重复的数 for (int j = i + 1; j < n; ++j) { if (j > i + 1 && nums[j] == nums[j - 1]) continue; // 注意跳过重复的数 int l = j + 1, r = n - 1; int t = target - nums[i] - nums[j]; while (l < r) { int s = nums[l] + nums[r]; if (s < t) { do { ++l; } while (l < r && nums[l] == nums[l - 1]); } elseif (s > t) { do { --r; } while (l < r && nums[r] == nums[r + 1]); } else { res.push_back({nums[i], nums[j], nums[l], nums[r]}); do { ++l; } while (l < r && nums[l] == nums[l - 1]); do { --r; } while (l < r && nums[r] == nums[r + 1]); } } } } return res; } };