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 ...
随机推荐
- 我学C的那些年[ch01]:浅淡C语言的编译过程
前几天大致学习了C语言的编译过程,那么今天就和大家分享一下 首先,编译C语言,需要一个文本编辑器(windows自带的也行),和一个MinGW编译器(需要配置环境),就可以将.c文件编译成.exe文件 ...
- bzoj3637: Query on a tree VI
Description You are given a tree (an acyclic undirected connected graph) with n nodes. The tree node ...
- 转:PHP的线程安全ZTS与非线程(NTS)安全版本的区别
原文来自于:http://blog.sina.com.cn/s/blog_94c21e8f0101s2ic.html Windows版的PHP从版本5.2.1开始有Thread Safe(线程安全)和 ...
- 延长FLASH和EEPROM芯片写入次数的小方法
开发电子产品时,常常需要断电后保存某些数据,这就需要使用 FLASH或EEPROM芯片,这两种芯片,可擦除的次数是有限制的,通常FLASH为10万次,EEPROM要多一点,为100万甚至1000万次. ...
- 第二章 Stream API
引例: 1 List<String> strList = Arrays.asList("zhaojigang","nana","tiany ...
- mysql 树形数据,层级数据Managing Hierarchical Data in MySQL
原文:http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ 引言 大多数用户都曾在数据库中处理过分层数据(hiera ...
- bzoj3505
ans=C((n+1)*(m+1),3)-三点一线的情况横线竖线我们可以先去掉然后考虑斜线,由于对称性我们只要考虑斜率大于0的即可有一个很显然的结论,但两点坐标差为x,y时,这条线段上的点数为gcd( ...
- Android RSA加密对象数据
前几天说了手头项目用MD5和双向加密DES来进行加密的,因为产品是在不断的改造中完善的.考虑到DES和MD5加解密还是会存下一定的风险,于是产品一拍,不行,听你们说MD5加密是不安全的,咱们产品以后做 ...
- poj3186 Treats for the Cows
http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total S ...
- supesite 标签语法
http://blog.sina.com.cn/s/blog_a3c7706701018c8o.html