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).

同上题,没上一题难,比葫芦画瓢即可

JAVA实现:

static public int threeSumClosest(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 j = i + 1, k = nums.length - 1;
while (j < k) {
if (Math.abs(nums[i] + nums[j] + nums[k] - target) < Math.abs(result - target))
result = nums[i] + nums[j] + nums[k];
if (nums[i] + nums[j] + nums[k] < target)
j++;
else if (nums[i] + nums[j] + nums[k] > target)
k--;
else
return target;
//不加while循环亦可通过测试,但是时间会偏长一些
while (i < k && nums[i] == nums[i + 1])
i++;
}
}
return result;
}

C++:

 class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int result = nums[] + nums[] + nums[];
sort(nums.begin(),nums.end());
for (int i = ; i < nums.size() - ; i++) {
int j = i + , k = nums.size() - ;
while (j < k) {
if (abs(nums[i] + nums[j] + nums[k] - target) < abs(result - target))
result = nums[i] + nums[j] + nums[k];
if (nums[i] + nums[j] + nums[k] < target)
j++;
else if (nums[i] + nums[j] + nums[k] > target)
k--;
else
return target;
while (i < k && nums[i] == nums[i + ])
i++;
}
}
return result;
}
};

【JAVA、C++】LeetCode 016 3Sum Closest的更多相关文章

  1. 【JAVA、C++】LeetCode 015 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  2. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. 【JAVA、C++】LeetCode 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  5. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  7. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  8. 【JAVA、C++】LeetCode 006 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

随机推荐

  1. Shell编程中Shift的用法

    Shell编程中Shift的用法 位置参数可以用shift命令左移.比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1.$2.$3丢弃,$0不移动.不带参数的shif ...

  2. Yii2的view需要链接跳转

    添加 use yii\helpers\Url; view中的连接 <?= Url::toRoute('post/index');?>//post为你的当前控制器名,index为view模版

  3. Android4.4中不能发送SD卡就绪广播

    当在Android上进行图片的扫描功能开发时一般会使用:sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(“file:// ...

  4. Oracle写函数读写日志实例

    1.用DBA登录赋权限create or replace directory D_OUTPUT as 'D:\TEMP'; grant read,write on directory D_OUTPUT ...

  5. 3.Android之单选按钮RadioGroup和复选框Checkbox学习

    单选按钮和复选框在实际中经常看到,今天就简单梳理下. 首先,我们在工具中拖进单选按钮RadioGroup和复选框Checkbox,如图: xml对应的源码: <?xml version=&quo ...

  6. 【BZOJ-2809】dispatching派遣 Splay + 启发式合并

    2809: [Apio2012]dispatching Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2334  Solved: 1192[Submi ...

  7. codevs1003 电话连线

    题目描述 Description 一个国家有n个城市.若干个城市之间有电话线连接,现在要增加m条电话线(电话线当然是双向的了),使得任意两个城市之间都直接或间接经过其他城市有电话线连接,你的程序应该能 ...

  8. html,body { margin:0; padding:0;border:0}

    body,html /* 设置窗口DIV为浏览器大小*/ { margin:; padding:; height:100%; } 下面代码 <!DOCTYPE html> <html ...

  9. java连接mysql(三)

    事务的四大特性(ACID) 原子性(Atomicity) 原子性是指事务是一个不可分割的工作单位,事务中的操作要么全部成功,要么全部失败.比如在同一个事务中的SQL语句,要么全部执行成功,要么全部执行 ...

  10. 学习使用Robot Framework自动化测试框架-web元素定位

    转:http://blog.csdn.net/u012145166/article/details/50342569 1.name和id 其中使用到了name和id定位.但有时候由于开发人员的疏忽或者 ...