C#解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 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的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- LeetCode——16. 3Sum Closest
一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...
- 蜗牛慢慢爬 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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- X86架构与ARM架构比较
引言 CPU是怎样运作的? CPU的运作与人脑的运作差不多.先谈一下人这个系统的工作方式.眼镜.耳朵.舌头.皮肤等等感觉器官接收到“触觉”,把信息传给大脑,大脑把信息处理后,把处理结果送给手.脚.嘴等 ...
- Windows开发技术的历史
原文地址:http://www.kuqin.com/windows/20090315/40172.html Windows已经有22年的历史,这22年来,微软官方主力推行的编程语言与API有四个分水岭 ...
- 【Java】Java 序列化的高级认识
如果你只知道实现 Serializable 接口的对象,可以序列化为本地文件.那你最好再阅读该篇文章,文章对序列化进行了更深一步的讨论,用实际的例子代码讲述了序列化的高级认识,包括父类序列化的问题.静 ...
- SharePoint 软件边界及限制
摘自technet http://technet.microsoft.com/zh-cn/library/cc262787.aspx
- angular2 学习笔记 (Typescript - Attribute & reflection)
refer : https://www.npmjs.com/package/reflect-metadata refer : https://www.typescriptlang.org/docs/h ...
- Building a Space Station
poj2031:http://poj.org/problem?id=2031 题意:就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通.如果两个球有重叠的部分则算为已连通, ...
- (step4.3.8)hdu 2181(哈密顿绕行世界问题——DFS)
题目大意:通俗点讲就是,输出所有从m城市出发,便利所有城市之后又能回到m城市的序列...... 解题思路:DFS 1)用map[][]来存储城市之间的连通情况.用used[]存储某个城市的使用情况(即 ...
- bzoj3230
以前觉得这题好难,现在觉得这题还是挺简单首先看到类似LCP问题不难想到后缀数组吧前后的相似需要我们分别做一个后缀数组和“前缀数组”(就是把字符串反向然后跑后缀数组)这道题的难点就在于如何确定子串是什么 ...
- WordPress 3.5.1 crypt_private()远程拒绝服务漏洞(CVE-2013-2173)
漏洞版本: WordPress 3.5.1 漏洞描述: BUGTRAQ ID: 60477 CVE(CAN) ID: CVE-2013-2173 WordPress是一种使用PHP语言和MySQL数据 ...
- (转载)Linux定时任务cron配置
(转载)http://blog.csdn.net/jbgtwang/article/details/7995801 实现linux定时任务有:cron.anacron.at等,这里主要介绍cron服务 ...