双指针
| #include <vector> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| vector<int> sortArrayByParityII(vector<int> &nums) { |
| int even = 0; |
| int odd = 1; |
| while (even < nums.size() - 1 && odd < nums.size()) { |
| |
| while (even < nums.size() - 1 && ((nums[even] & 1) == 0)) even += 2; |
| |
| while (odd < nums.size() && ((nums[odd] & 1) != 0)) odd += 2; |
| |
| if (even < nums.size() - 1 && odd < nums.size()) swap(nums[even], nums[odd]); |
| } |
| return nums; |
| } |
| }; |
- 要求 不修改 数组
nums
且只用常量级 O(1)
的额外空间。
| #include <vector> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| |
| int findDuplicate(vector<int> &nums) { |
| int slow = 0, fast = 0; |
| slow = nums[slow]; |
| fast = nums[nums[fast]]; |
| while (slow != fast) { |
| |
| slow = nums[slow]; |
| |
| fast = nums[nums[fast]]; |
| } |
| fast = 0; |
| while (slow != fast) { |
| slow = nums[slow]; |
| fast = nums[fast]; |
| } |
| return slow; |
| } |
| }; |
| #include <vector> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| int trap(vector<int> &height) { |
| int n = height.size(); |
| |
| int maxHeight = 0; |
| for (const auto &item: height) |
| maxHeight = max(maxHeight, item); |
| |
| int res = 0; |
| |
| for (int level = 1; level <= maxHeight; ++level) { |
| int i = 0; |
| |
| while (i < n && height[i] < level) i++; |
| |
| if (i >= n) break; |
| |
| for (int water = 0; i < n; i++) { |
| if (height[i] < level) { |
| |
| water++; |
| } else if (height[i] >= level) { |
| |
| |
| res += water; |
| water = 0; |
| } |
| } |
| } |
| return res; |
| } |
| }; |
| #include <vector> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| |
| int trap(vector<int> &height) { |
| int n = height.size(); |
| int res = 0; |
| |
| |
| int leftMax = height[0]; |
| |
| vector<int> leftMaxArr(n); |
| |
| for (int i = 1; i <= n - 2; ++i) { |
| leftMaxArr[i] = leftMax; |
| leftMax = max(leftMax, height[i]); |
| } |
| |
| |
| int rightMax = height[n - 1]; |
| vector<int> rightMaxArr(n); |
| for (int i = n - 2; i >= 1; i--) { |
| rightMaxArr[i] = rightMax; |
| rightMax = max(rightMax, height[i]); |
| } |
| |
| |
| for (int i = 1; i <= n - 2; ++i) |
| res += max(0, min(leftMaxArr[i], rightMaxArr[i]) - height[i]); |
| return res; |
| } |
| }; |
- 双指针优化掉 leftMaxArr、rightMaxArr(最优解)
| #include <vector> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| int trap(vector<int> &height) { |
| int n = height.size(); |
| int l = 1; |
| int r = n - 2; |
| |
| int lMax = height[0]; |
| |
| int rMax = height[n - 1]; |
| |
| int res = 0; |
| while (l <= r) { |
| if (lMax <= rMax) { |
| |
| res += max(0, lMax - height[l]); |
| lMax = max(lMax, height[l++]); |
| } else { |
| |
| res += max(0, rMax - height[r]); |
| rMax = max(rMax, height[r--]); |
| } |
| } |
| return res; |
| } |
| }; |
| #include <vector> |
| #include <stack> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| int trap(vector<int> &height) { |
| int res = 0; |
| stack<int> stk; |
| |
| for (int i = 0; i < height.size(); i++) { |
| |
| |
| while (!stk.empty() && height[i] > height[stk.top()]) { |
| int top = height[stk.top()]; |
| stk.pop(); |
| if (stk.empty()) break; |
| |
| int distance = i - stk.top() - 1; |
| |
| int smaller = min(height[stk.top()], height[i]); |
| res += distance * (smaller - top); |
| } |
| stk.emplace(i); |
| } |
| return res; |
| } |
| }; |
| #include <vector> |
| #include <algorithm> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| int numRescueBoats(vector<int> &people, int limit) { |
| sort(people.begin(), people.end()); |
| int res = 0; |
| for (int l = 0, r = people.size() - 1; l <= r; res++) { |
| int sum = l == r ? people[l] : people[l] + people[r]; |
| |
| if (sum <= limit) l++; |
| r--; |
| } |
| return res; |
| } |
| }; |
| #include <vector> |
| #include <algorithm> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| int maxArea(vector<int> &height) { |
| int res = 0; |
| for (int l = 0, r = height.size() - 1; l < r;) { |
| int water = (r - l) * min(height[l], height[r]); |
| res = max(res, water); |
| |
| |
| if (height[l] < height[r]) { |
| l++; |
| } else { |
| r--; |
| } |
| } |
| return res; |
| } |
| }; |
| #include <vector> |
| #include <algorithm> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| |
| |
| |
| |
| bool best(vector<int> &houses, vector<int> &heaters, int i, int j) { |
| return j == heaters.size() - 1 |
| || abs(heaters[j] - houses[i]) < abs(heaters[j + 1] - houses[i]); |
| } |
| |
| |
| int findRadius(vector<int> &houses, vector<int> &heaters) { |
| sort(houses.begin(), houses.end()); |
| sort(heaters.begin(), heaters.end()); |
| |
| int res = 0; |
| |
| for (int i = 0, j = 0; i < houses.size(); i++) { |
| while (!best(houses, heaters, i, j)) j++; |
| res = max(res, abs(heaters[j] - houses[i])); |
| } |
| return res; |
| } |
| }; |
- 原地映射:把值为 value 的元素映射到数组中下标为 value - 1 的位置
| #include <vector> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| |
| int firstMissingPositive(vector<int> &nums) { |
| int n = nums.size(); |
| for (int i = 0; i < n; ++i) { |
| |
| while (nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] != nums[i]) { |
| |
| |
| swap(nums[nums[i] - 1], nums[i]); |
| } |
| } |
| |
| for (int i = 0; i < n; ++i) |
| if (nums[i] != i + 1) |
| return i + 1; |
| |
| return n + 1; |
| } |
| }; |
| #include <vector> |
| #include <valarray> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| int firstMissingPositive(vector<int> &nums) { |
| int n = nums.size(); |
| |
| for (int i = 0; i < n; ++i) |
| if (nums[i] <= 0) |
| nums[i] = n + 1; |
| |
| for (int i = 0; i < n; ++i) { |
| |
| int value = abs(nums[i]); |
| |
| if (value <= n) nums[value - 1] = -abs(nums[value - 1]); |
| } |
| for (int i = 0; i < n; ++i) |
| if (nums[i] > 0) |
| return i + 1; |
| return n + 1; |
| } |
| }; |
| #include <vector> |
| |
| using namespace std; |
| |
| class Solution { |
| public: |
| |
| int firstMissingPositive(vector<int> &nums) { |
| |
| int l = 0; |
| |
| |
| |
| int r = nums.size(); |
| |
| |
| while (l < r) { |
| if (nums[l] == l + 1) { |
| |
| l++; |
| } else if (nums[l] <= l || nums[l] > r || nums[nums[l] - 1] == nums[l]) { |
| |
| |
| |
| |
| swap(nums[l], nums[--r]); |
| } else { |
| |
| swap(nums[l], nums[nums[l] - 1]); |
| } |
| } |
| return l + 1; |
| } |
| }; |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/18450731
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步