leetcode 16. 3Sum Closest JAVA
题目:
给定一个包括n个整数的数组nums和一个目标值target。找到nums中的三个整数,使得他们之和与target最为接近。返回三个整数之和,假定每组输入只存在唯一答案

解题思路:
将nums数组重新排列,然后从i = 0开始遍历,分别取left = i + 1和right = nums.length - 1,将nums[i]、nums[left]和nums[right]三数之和与target差值的绝对值与diff比较,如果小于diff,则使sum赋值为此三数之和,并且将diff的值修改为三数之和与target差值的绝对值
class Solution {
public int threeSumClosest(int[] nums, int target) {
int diff = Integer.MAX_VALUE, result = 0;
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1, right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == target) {
return sum;
}
if (diff > Math.abs(target - sum)) {
diff = Math.abs(target - sum);
result = sum;
}
if (sum > target) { //因为sum > target,因此只需将right向左调整,此时sum的值则会减少,这样才能找到更接近的target的三数之和
right--;
} else {
left++; //因为sum <= target,因此只需将left向右调整,此时sum的值则会增加,这样才能找到更接近target的三数之和
}
}
}
return result;
}
}
leetcode 16. 3Sum Closest JAVA的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- [Leetcode][016] 3Sum Closest (Java)
题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, ...
- Java [leetcode 16] 3Sum Closest
题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- LeetCode 16. 3Sum Closest. (最接近的三数之和)
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- Leetcode 16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode] 16. 3Sum Closest 解题思路
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
随机推荐
- spring data jpa删除的使用方式
public interface UserRepository extends CrudRepository<User, Long> { Long deleteByLastname(Str ...
- 关于springboot中文件上传,properties配置
spring.http.multipart.enabled=true #默认支持文件上传. spring.http.multipart.file-size-threshold=0 #支持文件写入磁盘. ...
- struts2 action重定向action中文乱码处理
比如:Action方法productCategorySave()变量message,传递给Action方法productCategoryAdd(),当变量message为中文变量时,要进行编码设置,不 ...
- 关于IOS新手在安装cocoa pods失败,因为ruby版本过低的解决方法+ (void) {升级ruby}
http://blog.csdn.net/zhaoen95/article/details/51995520 现在: 使用 OS 系统, 正在学习iOS 需要配置cocoapods 命令行中显 ...
- linux之shell编程初步
#################适用于CentOS6################## #!/bin/bash ########################################## ...
- oracle 中时间类型 date 与 long 互转
我们在保存时间到数据库时,有时候会保存long型的数据,固定长度是13位,是用当前时间减去1970-01-01,再换算成毫秒得到的结果. 但是要考虑到时区问题中国的时区时+8区所以时间要加上8小时 o ...
- [SoapUI] 通过Groovy获取SoapUI当前Project所在的目录
import com.eviware.soapui.support.GroovyUtils def groovyUtils = new GroovyUtils( context ) def proje ...
- java中List的用法和实例详解
java中List的用法和实例详解 List的用法List包括List接口以及List接口的所有实现类.因为List接口实现了Collection接口,所以List接口拥有Collection接口提供 ...
- Web测试实践-任务进度-Day03
小组成员 华同学.郭同学.覃同学.刘同学.穆同学.沈同学 任务进度 在经过任务分配阶段后,大家都投入到了各自的任务中,以下是大家今天任务的进度情况汇总. 华同学 & 刘同学(任务1) 1.再对 ...
- java IO其他流
1.内存操作流,ByteArrayInputStream和 ByteArrayOutputStream 案例:将小写转化为大写 /* * 内存操作流,将大写字母转化为小写字母(ByteArrayInp ...