力扣hot100_双指针

hot100_283.移动零

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

请注意 ,必须在不复制数组的情况下原地对数组进行操作。

示例 1:

输入: nums = [0,1,0,3,12]
输出: [1,3,12,0,0]

示例 2:

输入: nums = [0]
输出: [0]

提示:

  • 1 <= nums.length <= 104
    
  • -231 <= nums[i] <= 231 - 1
    
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        int l = 0, r = 0;
        bool flag = true; 
        while(r < nums.size() && l < nums.size()){
            if(flag){
                //true时判断l指针时候指向第一个0元素,不是的话向后寻找第一个0,找到之后将flag设为false,移动r指针
               if(nums[l] == 0) {
                   r = l + 1;
                   flag = false;
               }
               else {
                   l++;
               }
            }
            else { 
                //true时判断r指针时候指向第一个非0元素,找到第一个非零元素与l指针指向的元素互换
                if(nums[r] != 0) {
                    nums[l] = nums[r]; 
                    nums[r] = 0; 
                    flag = true; 
                    l++;
                }
                else {
                    r++;
                }
            }
        }
    }
};

int main(){
	Solution S;
	vector<int> nums = {0, 1, 0, 3, 12};
	S.moveZeroes(nums);
	for(int i = 0; i < nums.size(); i++){
		cout << nums[i] << " " ;
	}
	cout<<endl;
	return 0; 
}

hot100_11.盛最多水的容器

给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0)(i, height[i])

找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

返回容器可以储存的最大水量。

**说明:**你不能倾斜容器。

示例 1:

img

输入:[1,8,6,2,5,4,8,3,7]
输出:49 
解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。

示例 2:

输入:height = [1,1]
输出:1

提示:

  • n == height.length
  • 2 <= n <= 105
  • 0 <= height[i] <= 104
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
 
class Solution {
public:
    int maxArea(vector<int>& height) {
        int r = height.size() - 1, l = 0;
        int maxA = 0;
        while(r != l){
        	//每次移动指向元素值更小的那一个指针,每次计算一次面积 
        	maxA = max(maxA, (r - l) * min(height[r], height[l]));
			if(height[r] < height[l]){
				r -= 1;
			}
			else {
				l += 1;
			}
		}     	
		return maxA;
    }
};

int main(){
	vector<int> height = {1,8,6,2,5,4,8,3,7};
	Solution A;
	int res = A.maxArea(height);
	cout << res <<endl;
	return 0;
}

hot100_15.三数之和

给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != ji != kj != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请你返回所有和为 0 且不重复的三元组。

**注意:**答案中不可以包含重复的三元组。

示例 1:

输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
解释:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 。
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 。
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 。
不同的三元组是 [-1,0,1] 和 [-1,-1,2] 。
注意,输出的顺序和三元组的顺序并不重要。

示例 2:

输入:nums = [0,1,1]
输出:[]
解释:唯一可能的三元组和不为 0 。

示例 3:

输入:nums = [0,0,0]
输出:[[0,0,0]]
解释:唯一可能的三元组和为 0 。

提示:

  • 3 <= nums.length <= 3000
  • -105 <= nums[i] <= 105
#include<iostream>
#include<vector>
#include<algorithm> 
using namespace std;

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> result;
        int n = nums.size();
        // 排序,方便使用双指针
        sort(nums.begin(), nums.end());
        // 遍历每个元素作为三元组的第一个数
        for (int i = 0; i < n - 2; i++) {
            // 跳过重复的元素
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            int target = -nums[i];  // 目标值变为 -nums[i]
            int left = i + 1, right = n - 1;
            // 使用双指针查找剩下的两个数
            while (left < right) {
                int sum = nums[left] + nums[right];
                if (sum == target) {
                    result.push_back({nums[i], nums[left], nums[right]});
                    // 跳过重复的元素
                    while (left < right && nums[left] == nums[left + 1]) left++;
                    while (left < right && nums[right] == nums[right - 1]) right--;
                    left++;
                    right--;
                }
                else if (sum < target) {
                    left++;  // 需要增大和,所以左指针右移
                }
                else {
                    right--;  // 需要减小和,所以右指针左移
                }
            }
        }
        return result;
    }
};

int main(){
	vector<int> nums = {-1,0,1,2,-1,-4};
	Solution A;
	vector< vector<int> > res = A.threeSum(nums);
	for(int i = 0; i < res.size(); i++){
		for(int j = 0; j < res[i].size(); j++){
			cout << res[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}

hot100_42.接雨水

给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。

示例 1:

img

输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 

示例 2:

输入:height = [4,2,0,3,2,5]
输出:9

提示:

  • n == height.length
    1 <= n <= 2 * 104
    0 <= height[i] <= 105
    
#include<iostream>
#include<vector> 
using namespace std;

class Solution {
public:
    int trap(vector<int>& height) {
        int res = 0;
        int l_max = 0, r_max = 0; //维护左右最大值
        int l = 0, r = height.size() - 1; //左右“指针”
        while(l < r)
        {
            if(height[l] < height[r]){
                height[l] >= l_max ? l_max = height[l] : res += (l_max - height[l]);
                l++;
            }
            else{
                height[r] >= r_max ? r_max = height[r] : res += (r_max - height[r]);
                r--;
            }
        }
        return res;
    }
};

int main(){
	Solution A;
	vector<int> height = {0,1,0,2,1,0,1,3,2,1,2,1};
	cout << A.trap(height)<<endl;
	return 0;
}

解法2:单调栈

class Solution {
public:
    int trap(vector<int>& height) {
    int res = 0;
    stack<int> stack;
    for (int i=0; i<height.size(); i++) {
        //不单调时弹出栈顶元素
        while (!stack.empty() && height[stack.top()] < height[i]) {
            int top = stack.top();
            stack.pop();
            if (stack.empty()) {
                break;
            }
            int width = i - stack.top() - 1;
            int h = min(height[i], height[stack.top()]) - height[top];
            res += width * h;
        }
        //单调时将下标作为值压栈
        stack.push(i);
    }
    return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值