3Sum

标签(空格分隔): C++ 算法 LetCode 数组

每日算法——letcode系列


问题 Longest Common Prefix

Difficulty: Medium

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • The solution set must not contain duplicate triplets.

<pre>
For example, given array S = {-1 0 1 2 -1 -4},

A solution set is:
(-1, 0, 1)
(-1, -1, 2)

</pre>

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        
    }
};

翻译

三数和

难度系数:中等

给定一个有n个整数的数组S, 判断是否存在a, b, c三个元素满足a + b + c = 0? 找到数组中所有满足和为零的三个数(数组中不能有重复的三个数) 。

思路

如果随机找$C_n^3$种方案。假定满足条件的三个数为a,b,c,这三个数索引分别为i, j, k。先排序,从数组的头确定一个数a,b的索引比a大1,c为数组的尾,有三种情况

  • a + b + c = target 则i++ k-- 并把这三个数记录下来
  • a + b + c < target 则j++
  • a + b + c < target 则k--
    终止条件b < c。
    相当于确定两个,动一个,这样比较好确定方案

代码


class Solution {
public:
    vector<vector<int>> threeSum(vector<int> &nums) {
        vector<vector<int> > result;
        
        // 排序
        sort(nums.begin(), nums.end());
        int n = static_cast<int>(nums.size());
        
        for (int i = 0; i < n - 2; ++i) {
            // 去重复
            // 下面注释掉的去重复方案是错误的, 会把0,0,0也去掉
//            while (i < n -2 && nums[i] == nums[i+1]) {
//                i++;
//            }
             if (i>0 && nums[i-1] == nums[i]){
                 continue;
             }
            int j = i + 1;
            int k = n - 1;
            while (j < k) {
                if (nums[i] + nums[j] + nums[k] < 0){
                    // 去重复
                    while (j < k && nums[j] == nums[j+1]) {
                        j++;
                    }
                    j++;
                }
                else if (nums[i] + nums[j] + nums[k] > 0){
                     // 去重复
                    while (k > j && nums[k] == nums[k-1]) {
                        k--;
                    }
                    k--;
                }
                else{
                    result.push_back({nums[i], nums[j], nums[k]});
                    // 去重复
                    while (j < k && nums[j] == nums[j+1]) {
                        j++;
                    }
                    while (k > j && nums[k] == nums[k-1]) {
                        k--;
                    }
                    j++;
                    k--;
                }
            }
        }
        return result;
    }
};

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,968评论 0 23
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,779评论 0 33
  • 昨晚头沉沉的,迷迷糊糊睡了。做了一个超级长的梦。梦里先是准备考试,可是如何考都考不过,那么多的数学题目。还有小学暗...
    新之桐阅读 189评论 0 0
  • 西安,一座古老的城市。 初印象 厚重 充满了历史的归属感 跟我去过的任何一个城市都不一样 斑驳的树影 离逝的车辆 ...
    李文雅阅读 252评论 8 1
  • 待得久了就觉得很舒服,也很开心,尽管成长不是特别大,但这种感觉会好很多。对上对下考虑的不同,风格也会不一样。如果你...
    聂一一阅读 107评论 0 0