classSolution { public: intremoveElement(vector<int>& nums, int val){ int k = 0, n = nums.size(); for (int i = 0; i < n; i++) if (nums[i] != val) nums[k++] = nums[i]; return k; } };
classSolution { public: intremoveDuplicates(vector<int>& nums){ int k = 1, n = nums.size(); for (int i = 1; i < n; i++) if (nums[i]!= nums[k-1]) nums[k++] = nums[i]; return k; } intremoveDuplicates1(vector<int>& nums){ int k = 0, n = nums.size(); for (int i = 1; i < n; i++) if (nums[i]!= nums[k]) nums[++k] = nums[i]; return k+1; } };
classSolution { public: intminSubArrayLen(int target, vector<int>& nums){ int n = nums.size(), min = n + 1, sublen = 0, sum=0; int left = 0, right = 0; while (right < n) { sum += nums[right++]; while (target <= sum) { sublen = right - left; min = min < sublen ? min : sublen; sum -= nums[left++]; } } if(min==n+1) return0; elsereturn min; } };
classSolution { public: inttotalFruit(vector<int>& fruits){ int n = fruits.size(); unordered_map<int, int> cnt; int left = 0, right = 0, ans = 0; for (; right < n; right++) { cnt[fruits[right]]++; while (cnt.size() > 2) { cnt[fruits[left]]--; if (cnt[fruits[left]] == 0) { cnt.erase(fruits[left]); } left++; } ans = max(ans, right - left + 1); } return ans; } };