Given an array S of n integers, are there elements abc, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
  • The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.

    A solution set is:
    (-1,  0, 0, 1)
    (-2, -1, 1, 2)
    (-2,  0, 0, 2)我的答案:
public class Solution {
    public IList<IList<int>> FourSum(int[] nums, int target) {
        List<IList<int>> result = new List<IList<int>>();
        Array.Sort(nums);
        ;i<nums.Length-;i++)
        {
            ||nums[i]!=nums[i-])
            {
                ;m<nums.Length-;m++)
                {
                    || (nums[m]!=nums[m-])  )
                    {
                        ;
                        ;
                        ;

                        while(j<k)
                        {
                           List<int> res=new List<int>();
                           sum=nums[i]+nums[j]+nums[k]+nums[m];
                           if (sum==target)
                           {
                             ]{nums[i],nums[m],nums[j],nums[k]};
                             res.AddRange(ints);
                             result.Add(res);
                             ])
                             k--;
                             ])
                             j++;
                             k--;
                             j++;
                           }
                           else if (sum>target)
                               k--;
                           else
                               j++;
                        }
                    }

                }
            }
        }
        return result;
    }
}

总结:

1 这个答案虽然可以Accepted,但是运行时间太慢

2   int[] ints=new int[4]{nums[i],nums[m],nums[j],nums[k]}; res.AddRange(ints); 利用这两句可以向List中批量添加数据

3   Array.Sort(nums);可以直接对数组排序。

4 这个算法是在sum3的基础上改进的,在第二层for循环中要有条件(m-i)==1;确保当i!=0时候,就算m=i+1&&nums[m]==nums[i]的时候,也能进入if语句中

第二种,也是效率更高更好理解的一种方法,建议掌握这种方法,因为这种方法很容易可以迁移到其他个数相加的类似问题中:

public class Solution {
    public IList<IList<int>> FourSum(int[] nums, int target) {
        List<IList<int>> result = new List<IList<int>>();
        Array.Sort(nums);
        ;i<nums.Length-;i++)
        {
          int  target_3=target- nums[i];
          ;j<nums.Length-;j++)
          {
             int target_2=target_3- nums[j];
             ,back=nums.Length-;

             while(front<back)
             {
                 int sum=nums[front]+nums[back];
                 if(sum>target_2)
                   back--;
                 else if (sum<target_2)
                  front ++;
                  else
                  {
                     List<int> res=new List<int>();
                     ]{nums[i],nums[j],nums[front],nums[back]};
                     res.AddRange(ints);
                     result.Add(res);

                  ])  front++;
                  ])  back--;
                  front++;
                  back--;
                  }

             }

             <nums.Length-&&nums[j]==nums[j+])
                  j++;

          }
            <nums.Length-&&nums[i]==nums[i+])
                  i++;
        }
        return result;
    }
}

C#解leetcode 18. 4Sum的更多相关文章

  1. [LeetCode] 18. 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  2. LeetCode——18. 4Sum

    一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...

  3. LeetCode 18 4Sum (4个数字之和等于target)

    题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...

  4. [LeetCode] 18. 4Sum ☆☆

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  5. LeetCode 18. 4Sum (四数之和)

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  6. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

  7. Leetcode 18. 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  8. Java [leetcode 18]4Sum

    问题描述: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...

  9. [leetcode]18. 4Sum四数之和

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

随机推荐

  1. C++函数重载遇到了函数默认参数情况

    一.C++中的函数重载 什么是函数重载? 我的理解是: (1)用一个函数名定义不同的函数: (2)函数名和不同参数搭配时函数会有不同的含义: 举例说明: #include <stdio.h> ...

  2. 【算法】简单选择排序 O(n^2) 不稳定的 C语言

    简单选择排序 一.算法描述 假设序列中有N个元素: 第1趟找到第1到N个元素之间最小的一个,与第1个元素进行交换 第2趟找到第2到N个元素之间最小的一个,与第2个元素进行交换 第3趟找到第3到N个元素 ...

  3. C题 - A+B for Input-Output Practice (II)

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description You ...

  4. 弄懂css中单位px和em,rem的区别

              国内的设计师大都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什么优劣呢?         PX特点 1. IE无法调整那些使用px作为单位的字体大小 ...

  5. study note--(Education)

    some good temoplates: A child's education has never been about learning information and basic skills ...

  6. 跨进程发送消息数据(发送WM_COPYDATA消息,够简单的)

    1 //1.发送窗体 2 procedure TForm2.Button1Click(Sender: TObject); 3 var 4 h: HWND; 5 Size: Integer; 6 Cop ...

  7. Church encoding

    In mathematics, Church encoding is a means of representing data and operators in the lambda calculus ...

  8. kernel解读之 pick_next_rt_entity

    1328 static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq, 1329 struct rt_rq *rt_rq) 133 ...

  9. phpMyAdmin 完整路径泄露漏洞

    漏洞名称: phpMyAdmin 完整路径泄露漏洞 CNNVD编号: CNNVD-201307-650 发布时间: 2013-08-09 更新时间: 2013-08-09 危害等级: 中危   漏洞类 ...

  10. Light OJ 1027 - A Dangerous Maze(概率)

    题目大意: 你在一个迷宫里,你面前有n个门,你选择门的概率是一样的,每扇门有一个数字k, 加入这个数字是负数,那么这个门会花费你abs(k)分钟后把你带回原点, 假如这个数字是正数,他可以把你带出迷宫 ...