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. fragment 学习

    fragment需要id是必须属性 <fragment        android:id="@+id/frg1"        android:name="com ...

  2. Egret 纹理、计时器

    1. 九宫切 典型例子就是圆角矩形的拉伸问题. 先去P一张绿色的圆角矩形. private createGameScene():void { var box:egret.Bitmap = new eg ...

  3. 应用Oracle(Linux中的安装)

    Linux中安装Oracle,不同于windows. Linux在安装时,要作些必要的分区配置,以便进行Oracle的安装: 同时需要创建专门的数据库用户和组,并配置环境变量. root登录 使用 r ...

  4. 【Linux】鸟哥的Linux私房菜基础学习篇整理(七)

    1. test命令的测试功能.测试的标志:(1)关于文件类型的检测 test [-efdbcSpL] filename-e:该文件名是否存在:-f:该文件名是否为文件:-d:该文件名是否为目录:-b: ...

  5. Linux kernel ‘fib6_add_rt2node’函数安全漏洞

    漏洞名称: Linux kernel ‘fib6_add_rt2node’函数安全漏洞 CNNVD编号: CNNVD-201307-265 发布时间: 2013-07-16 更新时间: 2013-07 ...

  6. 【模拟】Codeforces 710A King Moves

    题目链接: http://codeforces.com/problemset/problem/710/A 题目大意: 国际象棋标准8X8棋盘,国王能往周围8个方向走.输入国王的位置,输出当前国王能往几 ...

  7. JavaScript & HTML5 Canvas 概览 更新时间2014-0411-1805

    HTML Canvas 坐标体系:矩形区域的左上角为坐标原点(0,0),向右为x轴,向下为y轴. 检测浏览器是否支持Canvas(IE系列从IE9开始支持): <!DOCTYPE html> ...

  8. 自动化Cobbler安装

    #install cobbler-server soft #date 2013.08.07 #disabled iptables and selinux /etc/init.d/iptables st ...

  9. 菜鸟学习SSH(一)——Struts实现简单登录(附源码)

    从今天开始,一起跟各位聊聊java的三大框架——SSH.先从Struts开始说起,Struts对MVC进行了很好的封装,使用Struts的目的是为了帮助我们减少在运用MVC设计模型来开发Web应用的时 ...

  10. java_method_Log输出日志的方法

    package cn.com.qmhd.tools; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigura ...