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. Insert Interval 面试题leetcode.

    刚开始做这个题的时候绕了好大的圈,对问题的分析不全面,没能考虑所有情况,做的很纠结.后来看了下大神的做法很受启发,改了改代码,最终提交了. public static ArrayList<Int ...

  2. windows7任务栏上的图标修复

    Technorati 标记: 疑难杂症   今天,我在使用Windows 7的时候,因为操作一些系统文件,发现桌面下角的个别正在运行的图标不见了,但是,我们如果再打开一个新程序,又会提醒你已经在运行了 ...

  3. ARM的BIN文件反汇编方法

    最近在调试uboot的代码时,用的新版本的uboot,lowlevel_init函数里是空的,而且在链接文件中也没有发现对lowlevel_init.o的链接.在bl lowlevel_init 之前 ...

  4. uboot使用tftp下载时出现“checksum bad”问题原因分析

    一.问题 二.原因分析 你的虚拟机是不是这样设置的呢? 如果是的话,请看下边的解释: 使用NAT模式,就是让虚拟系统借助NAT(网络地址转换)功能,通过宿主机器所在的网络来访问公网.也就是说,使用NA ...

  5. Markdown 测试

    量化派业务参考代码 测试二级标题 如果 merchant_id 是外部白条,则执行相关逻辑 if(order.getMerchantId() == Constants.BaitiaoMerchant. ...

  6. easyui_tree 复选框 动态加载树

    controller动态获取单位用户树 #region 下拉树菜单 /// <summary> /// 获取工作人员树菜单 /// </summary> /// <par ...

  7. emmet插件的导入与实用

    http://jingyan.baidu.com/article/ff4116259b057c12e48237b8.html http://www.iteye.com/news/27580 分享htm ...

  8. Windows Phone 学习笔记(一) 数据存储

    独立存储设置IsolatedStorageSetting private IsolatedStorageSettings _appSettings; public MainPage() { Initi ...

  9. plsql使用之debug

    1. 赋予一个普通用户debug权限 2. 收回权限 SQL> grant DEBUG CONNECT SESSION to bmp; Grant succeeded SQL> revok ...

  10. [BZOJ 1303] [CQOI2009] 中位数图 【0.0】

    题目链接:BZOJ - 1303 题目分析 首先,找到 b 的位置 Pos, 然后将数列中小于 b 的值赋为 -1 ,大于 b 的值赋为 1 . 从 b 向左扩展,不断算 Sum[i, b - 1] ...