博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3Sum Closest Total
阅读量:4454 次
发布时间:2019-06-07

本文共 1287 字,大约阅读时间需要 4 分钟。

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).
1 public class Solution { 2     //len-3 len-2 len-1 3     public int threeSumClosest(int[] num, int target) { 4         int len = num.length; 5         int min = Integer.MAX_VALUE; 6         int res = 0; 7         if(len<3) return 0; 8         Arrays.sort(num); 9         for(int i=0;i<=len-3;i++){10             int start = i+1;11             int end = len-1;12             while(start
Math.abs(sum-target)){19 min = Math.abs(sum-target);20 res = sum;21 }22 start++;23 }24 else{25 if(min>Math.abs(sum-target)){26 min = Math.abs(sum-target);27 res = sum;28 }29 end--;30 }31 }32 while(i
View Code

 

转载于:https://www.cnblogs.com/krunning/p/3555137.html

你可能感兴趣的文章
在ASP.NET页面中实现数据饼图
查看>>
在WPF中自定义控件(3) CustomControl (下)
查看>>
Pascal程序练习-与7无关的数
查看>>
Linux:cut命令...未完待续
查看>>
react实现svg实线、虚线、方形进度条
查看>>
Web
查看>>
那些容易忽略的事(1) -变量与运算符+
查看>>
九度oj 题目1252:回文子串
查看>>
(十一)tina | openwrt关闭调试串口(DEBUG UART)
查看>>
angularjs 使用angular-sortable-view实现拖拽效果(包括拖动完成后的方法使用)
查看>>
2015生命之旅---南京、南通、上海之行
查看>>
高精度练习之乘法(codevs_3117)
查看>>
小Z爱划水
查看>>
Qt Font
查看>>
2014年生日
查看>>
扫描目录下的文件并拼接在一起
查看>>
ELK 分布式日志处理 10.12
查看>>
Java虚拟机详解05----垃圾收集器及GC参数
查看>>
7. 单位,移动布局
查看>>
inux中bin与sbin目录的作用及区别介绍
查看>>