• Leetcode 3 sum && 3 sum closet&& 4 sum


     3 sum

    a+b+c=0

    class Solution {
    public:
        vector<vector<int> > threeSum(vector<int> &num) {
            vector<vector<int> >v;  
            vector<int>v1;
            sort(num.begin(),num.end());    
            for(int i=0;i<num.size();i++)
            {
                if(num.size()<3)break;
                if(num[i]>0)break;
                for(int a=i+1,b=num.size()-1;a<b;)
                {  
                    int sum=num[i]+num[a]+num[b];
                    if(sum==0)
                    {
                       v1.clear();
                       v1.push_back(num[i]);
                       v1.push_back(num[a]);
                       v1.push_back(num[b]);
                       if(count(v.begin(),v.end(),v1)==0)
                       v.push_back(v1);
                       a++;b--;
                     }
                     if(sum<0)
                     {
                         a++;
                     }
                     if(sum>0){b--;}         
                }        
            }
            return v;
        }
    };
    

    3 sum closet

    class Solution {
    public:
        int threeSumClosest(vector<int> &num, int target) {
            int n=num.size();
            assert(n>=3);
            sort(num.begin(),num.end());
            int value;
            int sum=num[0]+num[1]+num[2];
            int diff=abs(sum-target);
            int temp=sum;
            for(int i=0;i<n-2;i++)
            {
                int a=i;
                int b=n-1;
                int m;
                value = target-num[a]-num[b];
                while(a<b)
                {    
                    m=a+(b-a)/2;           
                      if(value==num[m])return target;  
                      if(abs(num[m]-value)<diff)
                      {
                          diff=abs(num[m]-value);
                          temp=target-value+num[m];
                      }
                      if(num[m]>value)
                      {
                        b=m-1;
                      }
                      else
                      {
                        a=m+1;
                      }         
                }       
            }
            return temp;
        }
    };
            
    

     以上代码有问题,二分查找此处中间值不能等于边界值。

    class Solution { 
    public: 
        int threeSumClosest(vector<int> &num, int target) { 
            int n=num.size(); 
            assert(n>=3); 
            sort(num.begin(),num.end()); 
            int value; 
            int sum=num[0]+num[1]+num[2]; 
            int diff=abs(sum-target); 
            int temp=sum; 
            int i=0,j=n-1;
            while(i<n-2&&j>1&&i<j-1)
            { 
                int a=i; 
                int b=j; 
                int m; 
                value = target-num[a]-num[b]; 
                while(a<b-1) 
                {     
                    m=a+(b-a)/2;
                    if(value==num[m])return target;   
                    if(abs(num[m]-value)<diff) 
                    { 
                        diff=abs(num[m]-value); 
                        temp=target-value+num[m]; 
                    } 
                    if(num[m]>value) 
                    { 
                       b=m; 
                    } 
                    else
                    { 
                       a=m; 
                    }          
                } 
                if(value<0)j--;
                else if(value>0) i++;
                else if(value==0&&num[m]>0)j--;
                else i++;
            } 
            return temp; 
        } 
    };  

    4 sum

    定义一对指针,指向两头。再定义一对指针,指向中间的两个元素。加起来看看跟target比较一下。再决定内部指针怎么移动

    class Solution {
    public:
        vector<vector<int> > fourSum(vector<int> &num, int target) {
            vector<int>v;
            vector<vector<int>>v1;
            int len=num.size();
            if(len<=3)return v1;
            sort(num.begin(),num.end());
            for(int i=0;i<len-3;i++)
            {
                for(int k=len-1;k>i+2;k--)
                {
                    int ab=num[i]+num[k];
                    int c=target-ab;
                    int m=i+1,n=k-1;
                    for(;m<n;)
                    {
                        int sum=num[m]+num[n];
                        if(sum==c)
                        {
                            v.clear();
                            v.push_back(num[i]);
                            v.push_back(num[m]);
                            v.push_back(num[n]);
                            v.push_back(num[k]);
                            if(count(v1.begin(),v1.end(),v)==0)
                            v1.push_back(v);
                            m++;n--;
                        }
                        else if(sum<c)
                        {
                            m++;
                        }
                        else n--;
                    }
                }
            }
            return v1;      
        }
    };
    
  • 相关阅读:
    WinForm窗体传值 总结
    SQLServer遍历数据库所有表及统计表数据总数
    GridView合并行代码
    日期转换成字符串
    flex与js交互浅析
    九,query task
    八,graphics
    十,Find and Identity
    转移批令
    db dw dd 和 dup
  • 原文地址:https://www.cnblogs.com/tgkx1054/p/3018459.html
Copyright © 2020-2023  润新知