前言
一个本硕双非的小菜鸡,备战24年秋招,计划二刷完卡子哥的刷题计划,加油!
二刷决定精刷了,于是参加了卡子哥的刷题班,训练营为期60天,我一定能坚持下去,迎来两个月后的脱变的,加油!
推荐一手卡子哥的刷题网站,感谢卡子哥。代码随想录
一、11. 盛最多水的容器
11. 盛最多水的容器
Note:双指针
class Solution {
public:
int maxArea(vector<int>& height) {
int leftIndex = 0, rightIndex = height.size() - 1;
int res = 0;
while (leftIndex < rightIndex) {
int ans = min(height[leftIndex], height[rightIndex]) * (rightIndex - leftIndex);
res = max(ans, res);
if (height[leftIndex] <= height[rightIndex])
leftIndex++;
else
rightIndex--;
}
return res;
}
};
二、41. 缺失的第一个正数
41. 缺失的第一个正数
Note:神思想,利用-1原地哈希
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int size = nums.size();
if (find(nums.begin(), nums.end(), 1) == nums.end())
return 1;
for (int i = 0; i < size; i++) {
if (nums[i] <= 0 || nums[i] > size)
nums[i] = 1;
}
for (int i = 0; i < size; i++) {
int num = abs(nums[i]) - 1;
nums[num] = -abs(nums[num]);
}
for (int i = 0; i < size; i++) {
if (nums[i] > 0)
return i + 1;
}
return size + 1;
}
};
三、42. 接雨水
42. 接雨水
Note:双指针
class Solution {
public:
int trap(vector<int>& height) {
int ans = 0;
int left = 0, right = height.size() - 1;
int leftMax = 0, rightMax = 0;
while (left < right) {
leftMax = max(leftMax, height[left]);
rightMax = max(rightMax, height[right]);
if (height[left] < height[right]) {
ans += leftMax - height[left];
++left;
} else {
ans += rightMax - height[right];
--right;
}
}
return ans;
}
};
总结
hot前100的三道题