思路一为遍历:

public int thirdSolution(int[] nums, int target) {
int result = nums[0] + nums[1] + nums[2];
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
int start = i + 1, end = nums.length - 1;
while (start < end) {
int tmp = nums[i] + nums[start] + nums[end];
if (tmp < target) {
start++;
}
if (tmp > target) {
end--;
}
if (tmp == target) {
return tmp;
}
if (Math.abs(tmp - target) < Math.abs(result - target)) {
result = tmp;
}
}
}
return result;
}

整体思路二为将threeSum将为twoSum即可

public int solution(int[] nums, int target) {
if (nums.length == 3) {
return nums[0] + nums[1] + nums[2];
} else {
Arrays.sort(nums);
int r = 10000;//此两处10000为省事而设,如果严谨应该大致找到其中的一个较大距离
int distance = 10000;
for (int i = 0; i < nums.length; i++) {
int[] temarray = new int[nums.length - 1];
System.arraycopy(nums, 0, temarray, 0, i);
System.arraycopy(nums, i + 1, temarray, i, nums.length - i - 1);
int tem = twoSumClosest(temarray, target - nums[i]) + nums[i];
int temdistance = tem - target;
if (temdistance < 0) {
temdistance = -temdistance;
} else if (temdistance == 0) {
return tem;
}
if (temdistance < distance) {
r = tem;
distance = temdistance;
}
}
return r;
}
} private int twoSumClosest(int[] nums, int target) {
int l = 0, r = nums.length - 1;
int min = nums[r] + nums[l];
int distance;
if (min - target > 0) {
distance = min - target;
} else {
distance = target - min;
}
while (l < r) {
if (nums[l] + nums[r] == target)
return nums[l] + nums[r];
if (nums[l] + nums[r] < target) {
if (target - (nums[l] + nums[r]) < distance) {
min = nums[l] + nums[r];
distance = target - (nums[l] + nums[r]);
}
l++;
continue;
}
if (nums[l] + nums[r] > target) {
if ((nums[l] + nums[r]) - target < distance) {
min = nums[l] + nums[r];
distance = (nums[l] + nums[r]) - target;
}
r--;
continue;
}
}
return min;
}

本质上讲两种思路没有区别

leetcode 日记 3sumclosest java的更多相关文章

  1. leetcode 日记 4sum java

    整体思路同之前的一样,依然采取降低维度的方式进行 public List<List<Integer>> solution(int nums[], int target) { L ...

  2. 2017/11/3 Leetcode 日记

    2017/11/3 Leetcode 日记 654. Maximum Binary Tree Given an integer array with no duplicates. A maximum ...

  3. 2017/11/22 Leetcode 日记

    2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...

  4. 2017/11/21 Leetcode 日记

    2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...

  5. 2017/11/13 Leetcode 日记

    2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...

  6. 2017/11/20 Leetcode 日记

    2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...

  7. 2017/11/9 Leetcode 日记

    2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...

  8. 2017/11/7 Leetcode 日记

    2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...

  9. 2017/11/6 Leetcode 日记

    2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...

随机推荐

  1. 十七、Java基础---------集合框架之Map

    前两篇文章中介绍了Collection框架,今天来介绍一下Map集合,并用综合事例来演示. Map<K,V> Map<K,V>:Map存储的是键值对形式的元素,它的每一个元素, ...

  2. Scala-Trait:混入与多态

    Scala 的 Trait 结合了抽象类与接口的能力,通过混入来获得灵活的多态能力. 代码如下所示: FileAbility 提供了读取文件.处理文件的能力, 其中继承一个空实现的 Trait:Lin ...

  3. iOS使用textfield注意的细节

    一般做登录界面或者要填写表之类的页面会经常使用到textfield.使用很简单,但是其实他有很多小的处理细节,这回让你显得有经验,交互性很好.在这里呢,我就直接拿stroyboard中的截图来说. c ...

  4. 基于webrtc的资源释放问题(一)

    基于webrtc的资源释放问题(一) ——重复释放webrtc的相关资源 背景: 视频通讯大都只是作为一个功能存在于各种应用中,比如微信,qq .既然只是应用的一部分,这样就涉及反复的开启和关闭视频通 ...

  5. android界面横屏和竖屏的切换

    关于android横屏和竖屏的切换网上给了很多种.但是有些介绍的方法都是在android旧版本上. 我现在把握用到的情况写下来用于备忘: android 版本:4.0 - 4.4 要求:android ...

  6. [转]Struts1.x系列教程(1):用MyEclipse开发第一个Struts程序

    转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/13/251101.html 本系列教程将详细介绍Struts 1.x的基本原理和使用方法, ...

  7. 2015弱校联盟(1) -J. Right turn

    J. Right turn Time Limit: 1000ms Memory Limit: 65536KB frog is trapped in a maze. The maze is infini ...

  8. VIM 常用错误解决

    1.option ‘omnifunc’ is not set 错误: vim7下Omni completion默认情况下是没有开启的,有时候自定义的vimrc文件会实现自动补齐,例如vim-autoc ...

  9. C中测试时间代码

  10. Scrum Meeting 7-20151209

    任务安排 姓名 今日任务 明日任务 困难 董元财 服务器购买记录接口 请假(编译攻坚) 无 胡亚坤 发布记录和购买记录 请假(编译攻坚) 无 刘猛 完成Scrum Meeting 请假(编译攻坚) 无 ...