2019.6.5 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新)
github:https://github.com/ChopinXBP/LeetCode-Babel
之前有做过两数之和的问题LeetCode(1):两数之和 Two Sum(Java)
两数之和问题的求解可以在有序数组中设定首尾指针逼近求解。三数之和和四数之和也是同样的思路:
先排序数组。遍历元素i时(相同大小的i均跳过),在i之后的数组中,用首尾逼近法寻找合为-nums[i]的两数之和。
最接近的三数之和思路相同,只需要再定一个distance代表三数之和与target的差值大小即可。最后返回distance最小的组合结果即可。
传送门:三数之和
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:The solution set must not contain duplicate triplets.
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
传送门:最接近的三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。
例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.
与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).
传送门:四数之和
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:The solution set must not contain duplicate quadruplets.
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
注意:答案中不可以包含重复的四元组。
示例:
给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。
满足要求的四元组集合为:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
三数之和 & 四数之和
/**
*
* @author ChopinXBP
* Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0?
* Find all unique triplets in the array which gives the sum of zero.
* Note: The solution set must not contain duplicate triplets.
* 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
* 注意:答案中不可以包含重复的三元组。
*
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class ThreeSum {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums = {-1, 0, 1, 2, -1, -4};
List<List<Integer>> result = threeSum(nums);
for(int i = 0; i < result.size(); i++) {
for(int j = 0; j < result.get(i).size(); j++) {
System.out.print(result.get(i).get(j) + " ");
}
System.out.println(" ");
}
}
//先排序数组,遍历数组的每一个元素nums[i],在i之后的数组中找合为-nums[i]的两个数,两数之和可以用首尾逼近法求解。
public static List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new LinkedList<>();
if(nums == null || nums.length < 3) return result;
Arrays.sort(nums);
for(int i = 0; i < nums.length - 2; i++) {
//跳过相同元素
if(i > 0 && nums[i] == nums[i - 1])
continue;
//设定首尾指针逼近查找
int begin = i + 1;
int end = nums.length - 1;
int num = -nums[i];
while(begin < end) {
if(nums[begin] + nums[end] == num) {
//将三个数字初始化为Arrays并存入LinkedList3
//result.add(new ArrayList(Arrays.asList(num[i], num[begin], num[end])));
result.add(Arrays.asList(nums[i], nums[begin], nums[end]));
begin++;
end--;
//跳过相同元素
while(begin < end && nums[begin] == nums[begin - 1]) {
begin++;
}
while(begin < end && nums[end] == nums[end + 1]) {
end--;
}
}
//若相加结果小于该数,begin前进
else if(nums[begin] + nums[end] < num) {
begin++;
}
//若相加结果大于该数,end后退
else {
end--;
}
}
}
return result;
}
//四数之和
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
if(nums == null || nums.length < 3) {
return result;
}
Arrays.sort(nums);
for(int i = 0; i < nums.length - 3; i++){
if(i > 0 && nums[i] == nums[i - 1]){
continue;
}
for(int j = i + 1; j < nums.length - 2; j++){
if(j > i + 1 && nums[j] == nums[j - 1]){
continue;
}
int begin = j + 1;
int end = nums.length - 1;
int targetNum = target - nums[i] - nums[j];
while(begin < end){
if(nums[begin] + nums[end] == targetNum){
result.add(Arrays.asList(nums[i], nums[j], nums[begin++], nums[end--]));
while(begin < end && nums[begin] == nums[begin - 1]){
begin++;
}
while(begin < end && nums[end] == nums[end] + 1){
end--;
}
}
else if(nums[begin] + nums[end] > targetNum){
end--;
}
else{
begin++;
}
}
}
}
return result;
}
}
最接近的三数之和
/**
*
* @author ChopinXBP
* Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target.
* Return the sum of the three integers. You may assume that each input would have exactly one solution.
* 给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。
*
*/
import java.util.Arrays;
public class ThreeSumClosest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums = {-1, 2, 1, -4};
int[] nums2 = {1, 1, -1};
System.out.println(threeSumClosest(nums, 1));
System.out.println(threeSumClosest(nums2, 1));
}
public static int threeSumClosest(int[] nums, int target) {
if(nums == null || nums.length < 3) return 0;
Arrays.sort(nums);
int distance = Integer.MAX_VALUE;
int result = 0;
for(int i = 0; i < nums.length - 2; i++) {
if(i > 0 && nums[i] == nums[i - 1])
continue;
int begin = i + 1;
int end = nums.length - 1;
int sum = target - nums[i];
while(begin < end) {
if(nums[begin] + nums[end] == sum) {
return target;
}
else if(nums[begin] + nums[end] > sum) {
if(Math.abs(nums[begin] + nums[end] - sum) < distance) {
distance = Math.abs(nums[begin] + nums[end] - sum);
result = nums[begin] + nums[end] + nums[i];
}
end--;
}
else {
if(Math.abs(nums[begin] + nums[end] - sum) < distance) {
distance = Math.abs(nums[begin] + nums[end] - sum);
result = nums[begin] + nums[end] + nums[i];
}
begin++;
}
}
}
return result;
}
}
#Coding一小时,Copying一秒钟。留个言点个赞呗,谢谢你#