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 ...
随机推荐
- 兼容PHP和Java的des加密解密代码分享
这篇文章主要介绍了兼容PHP和Java的des加密解密代码分享,适合如服务器是JAVA语言编写,客户端是PHP编写,并需要des加密解密的情况,需要的朋友可以参考下 作为一个iOS工程师来解决安卓的问 ...
- .net序列化和反系列化json与类型对象转换
先添加程序集: System.Web.Extensions(在 System.Web.Extensions.dll 中) 引用:using System.Web.Script.Serializati ...
- 【Web Service】WSDL文档
WSDL文档仅仅是一个简单的XML文档. 它包含一系列描述某个web service的定义. WSDL WSDL 是基于 XML 的语言,用于描述 Web services 以及如何访问它们. WSD ...
- Qt中暂停线程的执行(利用QMutex,超级简单明了)
在线程中定义一个信号量: QMutex pause;把run()函数中循环执行的部分用信号量pause锁住: void run() { while(1) { pause.lock(); //循环执行的 ...
- AlgorithmsI PA2: Randomized Queues and Deques Deque
本次作业考察利用array 或者linked list 实现规定时间复杂度的queue 和stack, 不能使用java 自带的stack 和queue. 目的是让我们掌握自己设计的函数的复杂度. D ...
- BZOJ 1072 [SCOI2007]排列perm
1072: [SCOI2007]排列perm Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1268 Solved: 782[Submit][Sta ...
- appendGrid
appendGrid appendGrid的使用
- 【图论】【宽搜】【染色】NCPC 2014 A Ades
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1787 题目大意: N个点M条无向边(N,M<=200000),一个节点只能有一个 ...
- HDOJ 2055 An easy problem
Problem Description we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, - f(Z) = 26, f(z) = -26; Giv ...
- python编译环境发掘——从IDLE到sublime到pycharm到Anaconda
一个好的编译器对于我们处理日常的科研很关键,好的编译器无论是从界面,字体风格,提示,调试等各方面都能从用户角度出发,提供最好的使用体验.Python本身自带的IDLE或者在CMD里进行操作和调试,对于 ...