思路一为遍历:

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. 反编译ILSpy 无法显式调用运算符或访问器 错误处理方法 转

    反汇编一个dll类库,导出的项目会报出很多bug,其中主要的就是“无法显式调用运算符或访问器”这个错误,看了一下,发现问题是在调用属性的时候,都 变成了方法,例如:pivotPoint.set_X(0 ...

  2. dataURI V.S. CSS Sprites 移动端

    英文原文:http://www.mobify.com/blog/css-sprites-vs-data-uris-which-is-faster-on-mobile/ 中文翻译:http://www. ...

  3. [转]关于NSAutoreleasePool' is unavailable: not available in automatic reference counting mode的解决方法

    转载地址:http://blog.csdn.net/xbl1986/article/details/7216668 Xcode是Version 4.2 Build 4D151a 根据Objective ...

  4. Linux phpwind论坛的安装

    1:新建文件夹phpwind

  5. 【Linux命令与工具】磁盘与目录的容量——df和du

    df(disk free):列出文件系统的整体磁盘使用量 用法: df [-akmhi] [目录或文件名] 参数: -a: 列出所有的文件系统,包括系统特有的/proc等文件系统 -k: 以KB的容量 ...

  6. linux atom 不支持中文

    linux atom 不支持中文 1.首先在ubuntu下安装泉驿正黑字体 sudo apt-get install ttf-wqy-* 2.Edit > Preferences > Se ...

  7. html回忆(一)

    1. 解决乱码,告诉浏览器 当前文档类型和编码 <meta http-equiv="Content-Type" content="text/html; charse ...

  8. 【Tomcat】Tomcat Session在Redis共享

    参考的优秀文章 Redis-backed non-sticky session store for Apache Tomcat 简单地配置Tomcat Session在Redis共享 我使用的是现有的 ...

  9. PHP-----类与对象,成员方法,成员属性,构造方法,析构方法

    php里面的类-----class XX{},通过类的定义,可以使用调用类里面的成员属性和成员方法. 对象---一个类就是一个对象,一个对象可以有多个属性,一个类可以有多个成员方法. 构造方法:一个类 ...

  10. ExceptionExtensions

    public static class ExceptionExtensions { public static IEnumerable<Exception> GetAllException ...