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. Beta版本——冲刺计划及安排

    我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...

  2. POJ1976A Mini Locomotive(01背包装+连续线段长度)

    A Mini Locomotive Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2485   Accepted: 1388 ...

  3. Java初学(四)

    一.图解二维数组 二.图解动态创建二维数组 三.杨辉三角练习: import java.util.Scanner; class Array2Demo{ //实现杨辉三角 public static v ...

  4. 批处理中的echo命令图文详解

    批处理中的echo命令图文详解 1. Echo 显示当前ECHO的状态:ECHO ON 或者ECHO OFF 2. ECHO ON 将ECHO状态设置为ON,将显示命令行,也就是前面的C:\>类 ...

  5. Python socket编程之一:

    soket 编程步骤 # -*- coding: utf-8 -*- ################################################################# ...

  6. tomcat架构

    很多开源应用服务器都是集成tomcat作为web container的,而且对于tomcat的servlet container这部分代码很少改动.这样,这些应用服务器的性能基本上就取决于Tomcat ...

  7. python学习笔记之module && package

    个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...

  8. Redis 数据库

    Redis 服务器     Remote Dictionay Server     Redis是一个key-value持久化产品,通常被称为数据结构服务器.   Redis的key是string类型: ...

  9. Ioc注解

    注解: 添加注解时,需要添加context的相关 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  10. c语言的头文件-不是c++类的头文件?

    下面的概述是参考的这篇文章:http://blog.csdn.net/bingxx11/article/details/7771437 c语言编程中也有,也需要头文件, 头文件不只是C++的类才需要! ...