Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

For example, given array S = {-1 2 1 -4}, and target = 1.

    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

答案:

public class Solution {
    public int ThreeSumClosest(int[] nums, int target) {
         )
             return nums.Sum();//计算数组所有元素的和

        List<int> list=new List<int>();
        ,sum=;
        ;i<nums.Length;i++)
            list.Add(nums[i]);

        list.Sort();

      answer=list[]+list[]+list[];//这个必须有,不能删除
      ;i<nums.Length-;i++)
      {
          ;
          ;

          while(j<k)
          {
             sum =  list[i]+list[j]+list[k];
             if (Math.Abs(target-answer)  >Math.Abs(target-sum) )
             {
                  answer=sum;
             if(answer==target)
                  return answer;
             }

             if (sum>target)
               k--;
             else
              j++;
          }
      }

     return answer;

    }
}

总结

1 c#的?:这个三元运算符,必须要赋值给一个变量,如int a=(1>2)?2:3; 此时a=3.而不能单独的(1>2)?2:3使用,此时会报错

错误 1 只有 assignment、call、increment、decrement 和 new 对象表达式可用作语句

2 List数组添加元素应该使用Add()方法,而不能够使用list[i]=1的这种赋值形式。

3 nums.Sum()是对数组的所用元素求和的运算

4 语句answer=list[1]+list[2]+list[3];这个必须有,不能删除。因为如果不加这句,按照程序来看,answer的初始值为0,此时如果出现了answer=0比初始的sum值优秀的情况下,answer的值将不会被更改。等for循环结束,answer仍然等于0,有可能数组的元素相加的最小值仍大于0,此时就会错误.给出一个错误的测试用例  【1,1,1,1】  ,0

而当加上answer=list[1]+list[2]+list[3]一句之后,可以确保返回值一定是数组其中三个元素和的最小值(即使程序while循环一直没有对answer在进行赋值)

5 这个算法的想法是,先对数组进行排序。for循环完成的工作是:在固定第一个元素的情况下,从第二个元素和最后一个元素开始遍历,如果找到了一个和值等于目标值,则返回该值;如果找不到的话,则将第二个元素后移或者最后一个元素前移,不断往复计算。

C#解leetcode 16. 3Sum Closest的更多相关文章

  1. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  2. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  3. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  4. LeetCode——16. 3Sum Closest

    一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...

  5. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

    题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  6. Array + two points leetcode.16 - 3Sum Closest

    题面 Given an array nums of n integers and an integer target, find three integers in nums such that th ...

  7. Leetcode 16. 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  8. Java [leetcode 16] 3Sum Closest

    题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...

  9. [LeetCode] 16. 3Sum Closest 解题思路

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

随机推荐

  1. openerp 中如何方便对搜索时间段

    以前为了方便的搜索时间区间,经常用wizard对方式,设置开始  结束时间,需要做大量对代码工作, 今天看了search view对组成, 可以用2个filter_domain 来做, 这样用户需要输 ...

  2. action 关联

    <act_window context="{'product_id': active_id}" id="act_stock_product_location_ope ...

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

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

  4. OC-深浅复制

    [OC学习-26]对象的浅拷贝和深拷贝——关键在于属性是否可被拷贝 对象的拷贝分为浅拷贝和深拷贝, 浅拷贝就是只拷贝对象,但是属性不拷贝,拷贝出来的对象和原来的对象共用属性,即指向同一个属性地址. 深 ...

  5. cf B Inna and Candy Boxes

    题意:输入n,然后输入n个数ai,再输入n个数bi,如果在1-ai中能找到两个数x,y,x和y可以相等,如果x+y=bi,答案加上x*y,否则减去1,让结果尽可能大,输出结果. #include &l ...

  6. Truck History(poj 1789)

    Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...

  7. LA3353

    感觉好久没做网络流这类的题目都不快会做了 网络流建模之前首先要分析性质 选择要求每个点恰属一个环就代表每个点在选择的图中,只有唯一入度和唯一出度 那就简单了,对n个点拆点,对于原图的边i-->j ...

  8. Linux kernel ‘ip6_sk_dst_check’函数拒绝服务漏洞

    漏洞名称: Linux kernel ‘ip6_sk_dst_check’函数拒绝服务漏洞 CNNVD编号: CNNVD-201307-070 发布时间: 2013-07-05 更新时间: 2013- ...

  9. P2032 「Poetize9」升降梯上

    描述 开启了升降梯的动力之后,探险队员们进入了升降梯运行的那条竖直的隧道,映入眼帘的是一条直通塔顶的轨道.一辆停在轨道底部的电梯.和电梯内一杆控制电梯升降的巨大手柄.Nescafe之塔一共有N层,升降 ...

  10. 去掉cell边框的简单办法

    实很简单,把backgroundView设置为一个空的View,然后就干净了 UIView *tempView = [[UIView alloc] init]; [cell setBackground ...