小white刷题记——LeetCodeHot100_15

该文章描述了一种算法方法,通过先对数组排序,然后使用双指针技术解决寻找数组中三个数之和为0的问题。在遍历过程中,通过调整左右指针避免值的重复,并优化了去重策略,避免相同元素导致的无效计算。
摘要由CSDN通过智能技术生成

 读题心路历程:这道题利用先排序的方式,确保无序的数组,可以变成从小到达的排序,方便利用双指针的方法进行遍历。像图中所示的一样。

遍历 i 的同时,定义两个左右指针,根据num[i]+nums[left]+nums[right]的值是否大于0来,移动left和right:

        ①当 num[i]+nums[left]+nums[right]<0,证明此时left指向的值太小了,需要大一点,所以需要让left++,指向下一个更大的值。

        ②当 num[i]+nums[left]+nums[right>0,证明此时right指向的值太大了,需要小一点,所以需要让right,指向上一个更小的值。

        以上操作的前提都是,至少有一组数据满足题意,所以num[i]+nums[left]+nums[right]的值是肯定会有等于0的时候。

这里需要注意的是:

        ①这个 i 的去重,什么意思呢?举例:如果数组是这样的—[-1,-1,0,1,1]那么nums[0]和nums[1]的值是相同的,所以判断完nums[0]之后,再去判断nums[1]就没必要了,就会出现重复的数组了。相当于又重新算了一遍nums[0]而已。

        ②当然,i 会出现重复,那么这个 left 和 right 当然也会发生重复了。因为,定义左右指针,所以他们其实是有规则的,当遍历的时候,出现right<left的时候就说明,左指针已经移动到右指针右边去了,这个时候就不需要遍历了,因为遍历也是之前重复的答案。

                1)left去重复的方式,如果发生nums[left] = nums[ left+1],这个时候是说明,left要往右移动的下一个元素和现在的是一样的,那就没必要移动到下一个元素了,因为会产生重复的数组和操作。

                2)right去重复的方式,同理nums[right] = nums[ right-1];

具体代码:

vector<vector<int>> threeSum(vector<int>& nums) {
	    vector<vector<int>> res;//定义一个二维vector数组,用来存放最后结果
	    sort(nums.begin(), nums.end());//排序
	    for (int i = 0; i < nums.size(); i++)
	    {
		    int left = i + 1;//定义左指针
		    int right = nums.size() - 1;//定义右指针
		    if (nums[i] > 0) { return res; }
		    if (i>0&&nums[i]==nums[i-1])
		    {
		    	continue;//有关 i 的去重判断。
		    }
		    while (left<right)
		    {
		    	if ((nums[i] + nums[left] + nums[right]) > 0)
			    {
			    	right--;
			    }
			    else if ((nums[i] + nums[left] + nums[right]) < 0)
			    {
			    	left++;
			    }
			    else
			    {
			    	res.push_back({ nums[i],nums[left],nums[right] });
			    	while (right > left && nums[right] == nums[right - 1]) { right--; }
			    	while (right > left && nums[left] == nums[left + 1]) { left++; }
		    		right--; //双指针同时收缩,因为找到一个答案后,指针必须都移动,才能找到下一个答案或者跳出while循环。
		    		left++;
		    	}
		    }
	    }
	    return res;
    }

“ # 设置按钮的背景颜色 self.m_button1.SetBackgroundColour('#0a74f7') self.m_button1.SetForegroundColour('white') self.m_button2.SetBackgroundColour('#0a74f7') self.m_button2.SetForegroundColour('white') self.m_button3.SetBackgroundColour('#0a74f7') self.m_button3.SetForegroundColour('white') self.m_button4.SetBackgroundColour('#238E23') self.m_button4.SetForegroundColour('white') self.m_button5.SetBackgroundColour('#238E23') self.m_button5.SetForegroundColour('white') self.m_button6.SetBackgroundColour('#238E23') self.m_button6.SetForegroundColour('white') self.m_button7.SetBackgroundColour('#6F4242') self.m_button7.SetForegroundColour('white') self.m_button8.SetBackgroundColour('#6F4242') self.m_button8.SetForegroundColour('white') self.m_button9.SetBackgroundColour('#6F4242') self.m_button9.SetForegroundColour('white') self.m_button10.SetBackgroundColour('#8E6B23') self.m_button10.SetForegroundColour('white') self.m_button11.SetBackgroundColour('#8E6B23') self.m_button11.SetForegroundColour('white') self.m_button12.SetBackgroundColour('#8E6B23') self.m_button12.SetForegroundColour('white') self.m_button13.SetBackgroundColour('#8E6B23') self.m_button13.SetForegroundColour('white') self.m_button14.SetBackgroundColour('#545454') self.m_button14.SetForegroundColour('white') self.m_button15.SetBackgroundColour('#545454') self.m_button15.SetForegroundColour('white') self.m_button16.SetBackgroundColour('#545454') self.m_button16.SetForegroundColour('white') self.m_panel1.SetBackgroundColour('white') # 设置面板的背景颜色”逐行解释代码
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值