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).
这个和3sum的思路一样
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).
这个和3sum的思路一样
public int threeSumClosest(int[] num, int target) { int result = 0; int temp = Integer.MAX_VALUE; if(num == null || num.length<3){ return 0; } Arrays.sort(num); for(int i = 0 ;i<num.length-1;i++){ int tar = target - num[i]; int end = num.length-1; int begin = i+1; while(end > begin){ if(tar> num[end]+num[begin]){ temp = Math.abs(temp)<Math.abs(tar- num[end] - num[begin])?temp:tar- num[end] - num[begin]; begin++; while(begin<num.length&&num[begin] == num[begin-1]){ begin++; } }else if(tar<num[end]+num[begin]){ temp = Math.abs(temp)<Math.abs(tar- num[end] - num[begin])?temp:tar- num[end] - num[begin]; end--; while(end>-1&&num[end] == num[end+1]){ end--; } }else { return target; } } } result = target - temp; return result; }
发表评论
-
Merge k Sorted Lists
2015-03-12 19:55 325Merge k sorted linked lists and ... -
Generate Parentheses
2015-03-12 19:50 363Given n pairs of parentheses, w ... -
Generate Parentheses
2015-03-05 22:39 0Given n pairs of parentheses, w ... -
Valid Parentheses
2015-03-05 22:33 307Given a string containing just ... -
Remove Nth Node From End of List
2015-03-05 22:31 338Given a linked list, remove the ... -
Letter Combinations of a Phone Number
2015-03-05 22:30 328Letter Combinations of a Phone ... -
4Sum
2015-03-05 22:26 310Given an array S of n integers, ... -
3Sum
2015-03-03 22:34 317Given an array S of n integers, ... -
Longest Common Prefix
2015-03-03 22:21 326Write a function to find the lo ... -
Roman to Integer
2015-03-03 22:20 326Given a roman numeral, convert ... -
Integer to Roman
2015-03-01 23:35 288Given an integer, convert it to ... -
Container With Most Water
2015-03-01 22:55 321Given n non-negative integers a ... -
Regular Expression Matching
2015-03-01 20:19 365Implement regular expression ma ... -
Palindrome Number
2015-02-13 22:08 329Determine whether an integer is ... -
String to Integer (atoi)
2015-02-13 11:07 340Implement atoi to convert a str ... -
Reverse Integer
2015-02-12 23:39 226Reverse digits of an integer. ... -
ZigZag Conversion
2015-02-12 23:37 253The string "PAYPALISHIRING ... -
Longest Palindromic Substring
2015-02-12 22:50 332Given a string S, find the long ... -
Add Two Numbers
2015-02-12 22:12 299You are given two linked lists ... -
Longest Substring Without Repeating Characters
2015-02-11 21:14 424[size=24px;]Longest Substring W ...
相关推荐
js代码-16. 3Sum Closest
16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum 19. Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 23. Merge k Sorted ...
18 3Sum Closest 57 19 String to Integer (atoi) 59 20 Merge Sorted Array 61 ... ... 231 Counting Bits 561 232 Maximum Product of Word Lengths 563 233 Gray Code 565 234 Permutations 567 235 Permutations...
3Sum Closest 4Sum Remove Element Move Zeroes Next Permutation Permutation Sequence Valid Sudoku Trapping Rain Water Rotate Image Plus One Climbing Stairs Set Matrix Zeroes Gas Station Candy Majority ...
[3 Sum Closest.java]( Sum Closest.java) Java 2个 [3 Sum.java]( Sum.java) Java 3 [4 Sum.java]( Sum.java) Java 4 Java 5 Java 6 [Balanced Binary Tree.java]( Binary Tree.java) Java 7 ...
[3Sum Smaller.java]( Smaller.java) Java 6 [4 Sum.java]( Sum.java) Java 7 Java 8 [添加和搜索 Word.java](和搜索 Word.java) Java 9 [添加Binary.java](Binary.java) Java 10 [加两个数II.java]( 两个数II....
java lru leetcode Java算法问题 托管来自 LintCode、LeetCode 等算法问题的 Java 解决方案。 一旦出现新问题或新测试用例,我将尝试...[3Sum Smaller.java]( Smaller.java) Java 6 [4 Sum.java]( Sum.java) 中等的 J
16 | [3 Sum Closest](https://leetcode.com/problems/3sum-closest/) | [C++](./C++/3sum-closest.cpp) [Python](./Python/3sum-closest.py) | _O(n^2)_ | _O(1)_ | Medium || Two Pointers 18| [4 Sum]...
leetcode 2 sum c leetcode 力扣(Leetcode)编程题,JavaScript版本。 编号 中文题目 ...3Sum 中等 16 3Sum Closest 中等 17 Letter Combinations of a Phone Number DFS 中等 18 4Sum 中等 19 Remo
lru cache ...3Sum Hash法转换2sum 9 3Sum Closest Sort +夹逼法 10 4Sum Sort +夹逼法 11 Remove Element 12 Next Permutation 公式 13 Permutation Sequence 公式 14 Valid Sudoku 15 Trapping Rain W
leetcode 530 ** LeetCode ...3Sum 016 3Sum Closest 017 Letter Combinations of a Phone Number 018 4Sum 020 Valid Parentheses 022 Generate Parentheses 028 Implement strStr() 031 Next Permutat
leetcode题库 LeetCode 题解合集 本仓库展示了LeetCode题库中部分题目的解法(持续更新),所有代码均采用C++编写...3Sum.cpp 最接近的三数之和 3Sum Closest .cpp 20 有效的括号 Valid Parentheses.cpp 22 括号生成 G
leetcode中文版 LeetCode # Title Chinese Tag Solution 1 Two Sum 两数之和 array,hash ...3 ...3Sum Closest 最接近的三数之和 two pointers,array 21 Merge Two Sorted Lists 合并两个有序链表 lin
3Sum.js 16 3Sum Closest.js 电话号码的 17 个字母组合.js 19 从 List.js 的末尾删除第 N 个节点 20 个有效括号.js 21 合并两个有序Lists.js 22 生成括号.js 23 合并 k 排序 Lists.js Pairs.js 中的 24 个交换节点 k...
3Sum |2012-01-17|16.4% 3Sum Closest|2012-01-18|26.8% 4Sum|2012-01-26 |21.3% 二进制加法|2012-04-02|25.6% 两个数相加|2011-11-01|22.6% 字谜|2012-03-19|23.1% 平衡二叉树|2012-10-08|32.1% 最佳时间买卖股票|...
答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...
leetcode 2 和 c LeetCode_record(C++) Leetcode 问题 我已经完成了解决方案和简要说明。 目前,我正在做热门面试问题。 完毕 ...15-3总和 16-3Sum Closest(无解,类似3Sum) 454-4和II 正在做 18-4和
3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7.Reverse Integer 8.String to Integer (atoi) 9....
java lru leetcode :ice_cream: ...Closest 20 Valid Parentheses 26 Remove Duplicates from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73