LeetCode-16.3Sum Closest
来源:互联网 发布:阿里云软件市场分成 编辑:程序博客网 时间:2024/06/10 09:12
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 int ThreeSumClosest(int[] nums, int target) { Array.Sort(nums); int front, back, sum,result; result= nums[0] +nums[1] + nums[2]; for (int i = 0; i < nums.Length-2; i++) { front = i + 1; back = nums.Length - 1; while (front < back) { sum = nums[i] + nums[front] + nums[back]; if (sum == target) return sum; if (Math.Abs(target-sum)< Math.Abs(target - result)) { result = sum; } if (sum > target) back--; else front++; } } return result; }
0 0
- [LeetCode]16.3Sum Closest
- LeetCode 16.3Sum Closest
- [leetcode] 16.3Sum Closest
- LeetCode-16.3Sum Closest
- Leetcode||16.3Sum Closest
- LeetCode 16.3Sum Closest
- LeetCode----- 16.3Sum Closest
- [leetcode]16.3Sum Closest(Java实现)
- 【leetcode】16.3Sum Closest 方法总结
- LeetCode: 3 Sum Closest
- leetcode - 3 sum closest
- leetcode 3Sum Closest
- LeetCode: 3Sum Closest
- [Leetcode]3Sum Closest
- Leetcode: 3SUM Closest
- LeetCode 3Sum Closest
- [Leetcode] 3Sum Closest
- LeetCode : 3Sum Closest
- 继承的应用
- 172. Factorial Trailing Zeroes
- 开发中犯的低级错误。。。
- c++ <new定位运算符>与<显式调用析构函数> - 练习题
- 纯css实现尖角图形
- LeetCode-16.3Sum Closest
- CSS样式设置--水平居中
- SDAU 1012
- java运行环境的配置
- Final关键字
- 字符串
- android视图学习---Android中将布局文件/View添加至窗口过程分析 ---- 从setContentView()谈起
- 基于划分的聚类----K-means算法使用(R语言)
- java连接数据库时加载驱动程序Class.forName(driveName)详解