zoukankan      html  css  js  c++  java
  • LeetCode 3Sum

    class Solution {
    public:
        vector<vector<int> > threeSum(vector<int> &num) {
            vector<vector<int> > res;
            int len = num.size();
            sort(num.begin(), num.end());
            vector<int> tmp(3, 0);
            for (int i=0; i<len; i++) {
                tmp[0] = num[i];
                if (i != 0 && num[i] == num[i-1]) continue; // skip dup
                int rs = 0 - num[i];
                int p = i + 1, q = len - 1;
                while (p<q) {
                    if (p != i+1 && num[p] == num[p-1]) {
                        p++;
                        continue;   // skip dup
                    }
                    if (q != len-1 && num[q] == num[q+1]) {
                        q--;
                        continue;   // skip dup
                    }
                    int s = num[p] + num[q];
                    if (s < rs) {
                        p++;
                    } else if (s > rs) {
                        q--;
                    } else {
                        tmp[1] = num[p];
                        tmp[2] = num[q];
                        res.push_back(tmp);
                        p++, q--;
                    }
                }
            }
            return res;
        }
    };

    先暴力一下吧

    第二轮:

    Given an array S of n integers, are there elements abc 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.
        For example, given array S = {-1 0 1 2 -1 -4},
    
        A solution set is:
        (-1, 0, 1)
        (-1, -1, 2)
    class Solution {
    public:
        vector<vector<int> > threeSum(vector<int> &num) {
            sort(num.begin(), num.end());
            vector<vector<int> > res;
            int len = num.size();
            if (len < 3) {
                return res;
            }
            for (int i=len-1; i>=2; i--) {
                if (i<len-1 && num[i] == num[i+1]) continue;
                int target = -num[i];
                int p = 0;
                int q = i-1;
                while (p < q) {
                    int t = num[p] + num[q];
                    if (t < target) {
                        p++;
                    } else if (t > target) {
                        q--;
                    } else {
                        if (!res.empty() 
                            && res.back()[0] == num[p]
                            && res.back()[1] == num[q]
                            && res.back()[2] == num[i]) {
                                
                        } else {
                            res.push_back(vector<int> ({num[p], num[q], num[i]}));
                        }
                        p++;
                    }
                }
            }
            return res;
        }
    };
  • 相关阅读:
    asp中动态include的方法
    asp存储过程使用大全
    用vb6写asp组件的简单例子
    asp中遍历一些对象(request,session,Application)
    查看ASP Session 变量的小工具
    层不能跨框架(包括TEXTAREA)显示的解决办法
    保存远程图片到本地 同时取得第一张图片并创建缩略图
    使用.Net开发asp组件
    使用ASP在IIS创建WEB站点
    解析notes自带的rtf javaapplet编辑器
  • 原文地址:https://www.cnblogs.com/lailailai/p/3669335.html
Copyright © 2011-2022 走看看