• 15. 3Sum(C++)


    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: 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]
    ]

    solution :

    class Solution {
    public:
      vector<vector<int>> threeSum(vector<int>& nums) {
        int len=nums.size();
        vector<vector<int>> solution;//(m);
        std::sort(nums.begin(),nums.end());
        for(int i=0;i<len;i++){
          int sum=-nums[i];
          int left=i+1;
          int right=len-1;
          while(left<right){
            int number=nums[left]+nums[right];
            if(sum>number){
              left++;
            }else if(sum<number){
              right--;
            }else{
              vector<int> tmp(3,0);
              tmp[0]=nums[i];
              tmp[1]=nums[left];
              tmp[2]=nums[right];
              solution.push_back(tmp);
              while(left<right && nums[left]==tmp[1]) left++;
              while(left<right && nums[right]==tmp[2]) right--;
            }
          }
          while(i+1<len && nums[i]==nums[i+1]) i++;
        }
        return solution;
      }
    };




  • 相关阅读:
    【权限维持】window服务端常见后门技术
    Empire安装和试用
    手把手教你打造自己的弱口令扫描工具(系统弱口令篇)
    Discuz!X 3.4 任意文件删除漏洞复现过程(附python脚本)
    【web端权限维持】利用ADS隐藏webshell
    Redis 未授权访问漏洞(附Python脚本)
    一句话木马:JSP篇
    一句话木马:ASPX篇
    一句话木马:ASP篇
    一句话木马:PHP篇
  • 原文地址:https://www.cnblogs.com/devin-guwz/p/6498574.html
Copyright © 2020-2023  润新知