当前位置:网站首页>[cute new problem solving] sum of four numbers
[cute new problem solving] sum of four numbers
2022-07-19 14:49:00 【InfoQ】
Title Description
- 0 <= a, b, c, d < n
- a、b、c and d Different from each other
- nums[a] + nums[b] + nums[c] + nums[d] == target
Problem solving instructions
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
// Array sorting
Arrays.sort(nums);
// The length of the array
int len = nums.length;
// result set
List<List<Integer>> ans = new ArrayList<>();
List<Integer> tmp = null;
for (int i=0; i<len; ++i) {
// Fix nums[i]
List<List<Integer>> res = threeSum(nums, i+1, target - nums[i]);
for (List<Integer> item : res) {
item.add(nums[i]);
ans.add(item);
}
// Skip repeating elements
while (i < len-1 && nums[i] == nums[i+1]) {
++i;
}
}
return ans;
}
/**
* Sum of three numbers , The input array is already in order
* In the array nums Find all sums equal to target Non repeating triples of
*/
public List<List<Integer>> threeSum(int[] nums, int start, int target) {
// The length of the array
int len = nums.length;
// result set
List<List<Integer>> ans = new ArrayList<>();
// A triplet in the result set
List<List<Integer>> tmp = null;
for (int i = start; i < len; ++i) {
// Fix nums[i] As the first element , adopt twoSum Get all the other two elements that meet the conditions
// Then form a triple together , Be careful ,twoSum Join the reference , The subscript at the beginning of the array start To set up
// by i+1, Can no longer contain nums[i]
tmp = twoSum(nums, i + 1, target - nums[i]);
for (List<Integer> item : tmp) {
item.add(nums[i]);
// Find a triplet that meets the conditions , And added to the result combination
ans.add(item);
}
// When something like [1,1,2,2,2,3,3] When there are arrays of repeating elements , To skip repeating elements
while (i < len - 1 && nums[i] == nums[i + 1]) {
++i;
}
}
return ans;
}
/**
* Sum of two numbers
* Input array nums It's in order , No need to repeat sorting , And subscript from start Start
* That is to find out from start Start to nums.length Between , All satisfaction and for target Non repeating binary of
*/
public List<List<Integer>> twoSum(int[] nums, int start, int target) {
// The length of the array
int len = nums.length;
// Left pointer , Point to the beginning of the array
int left = start;
// Right pointer , Point to the end of the array
int right = len - 1;
// Return results
List<List<Integer>> ans = new ArrayList<>();
// Start the cycle
while (left < right) {
int sum = nums[left] + nums[right];
// Save the first number of the current cycle , The latter is used for weight determination
int numLeft = nums[left];
// Save the second number of the current cycle , The latter is used for weight determination
int numRight = nums[right];
if (target == sum) {
// Find the target element , Save to result set
List<Integer> tmp = new ArrayList<>();
tmp.add(nums[left]);
tmp.add(nums[right]);
ans.add(tmp);
// When something like [1,1,2,2,2,3,3] When there are arrays of repeating elements , To skip repeating elements
// Because the title requires that the result set is not repeated
while (left < right && nums[left] == numLeft) {
left++;
}
// When something like [1,1,2,2,2,3,3] When there are arrays of repeating elements , To skip repeating elements
// Because the title requires that the result set is not repeated
while (left < right && nums[right] == numRight) {
right--;
}
} else if (target < sum) {
// When something like [1,1,2,2,2,3,3] When there are arrays of repeating elements , To skip repeating elements
// Reduce unnecessary cycles
while (left < right && nums[right] == numRight) {
right--;
}
} else if (target > sum) {
// When something like [1,1,2,2,2,3,3] When there are arrays of repeating elements , To skip repeating elements
// Reduce unnecessary cycles
while (left < right && nums[left] == numLeft) {
left++;
}
}
}
return ans;
}
}

target - nums[i]class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
// Array sorting
Arrays.sort(nums);
// The length of the array
int len = nums.length;
Long targetLong = (long)target;
// result set
List<List<Integer>> ans = new ArrayList<>();
List<Integer> tmp = null;
for (int i=0; i<len; ++i) {
// Fix nums[i]
List<List<Integer>> res = threeSum(nums, i+1, targetLong - nums[i]);
for (List<Integer> item : res) {
item.add(nums[i]);
ans.add(item);
}
// Skip repeating elements
while (i < len-1 && nums[i] == nums[i+1]) {
++i;
}
}
return ans;
}
/**
* Sum of three numbers , The input array is already in order
* In the array nums Find all sums equal to target Non repeating triples of
*/
public List<List<Integer>> threeSum(int[] nums, int start, long target) {
// The length of the array
int len = nums.length;
// result set
List<List<Integer>> ans = new ArrayList<>();
// A triplet in the result set
List<List<Integer>> tmp = null;
for (int i = start; i < len; ++i) {
// Fix nums[i] As the first element , adopt twoSum Get all the other two elements that meet the conditions
// Then form a triple together , Be careful ,twoSum Join the reference , The subscript at the beginning of the array start To set up
// by i+1, Can no longer contain nums[i]
tmp = twoSum(nums, i + 1, target - nums[i]);
for (List<Integer> item : tmp) {
item.add(nums[i]);
// Find a triplet that meets the conditions , And added to the result combination
ans.add(item);
}
// When something like [1,1,2,2,2,3,3] When there are arrays of repeating elements , To skip repeating elements
while (i < len - 1 && nums[i] == nums[i + 1]) {
++i;
}
}
return ans;
}
/**
* Sum of two numbers
* Input array nums It's in order , No need to repeat sorting , And subscript from start Start
* That is to find out from start Start to nums.length Between , All satisfaction and for target Non repeating binary of
*/
public List<List<Integer>> twoSum(int[] nums, int start, long target) {
// The length of the array
int len = nums.length;
// Left pointer , Point to the beginning of the array
int left = start;
// Right pointer , Point to the end of the array
int right = len - 1;
// Return results
List<List<Integer>> ans = new ArrayList<>();
// Start the cycle
while (left < right) {
int sum = nums[left] + nums[right];
// Save the first number of the current cycle , The latter is used for weight determination
int numLeft = nums[left];
// Save the second number of the current cycle , The latter is used for weight determination
int numRight = nums[right];
if (target == sum) {
// Find the target element , Save to result set
List<Integer> tmp = new ArrayList<>();
tmp.add(nums[left]);
tmp.add(nums[right]);
ans.add(tmp);
// When something like [1,1,2,2,2,3,3] When there are arrays of repeating elements , To skip repeating elements
// Because the title requires that the result set is not repeated
while (left < right && nums[left] == numLeft) {
left++;
}
// When something like [1,1,2,2,2,3,3] When there are arrays of repeating elements , To skip repeating elements
// Because the title requires that the result set is not repeated
while (left < right && nums[right] == numRight) {
right--;
}
} else if (target < sum) {
// When something like [1,1,2,2,2,3,3] When there are arrays of repeating elements , To skip repeating elements
// Reduce unnecessary cycles
while (left < right && nums[right] == numRight) {
right--;
}
} else if (target > sum) {
// When something like [1,1,2,2,2,3,3] When there are arrays of repeating elements , To skip repeating elements
// Reduce unnecessary cycles
while (left < right && nums[left] == numLeft) {
left++;
}
}
}
return ans;
}
}
边栏推荐
- keystore文件里PrivateKeyEntry错误配置导致TLS连接失败
- [Axi] interpret the additional signals of the Axi protocol (QoS signal, region signal, and user signal)
- Oracle - 锁
- Characteristics of DMA mode
- MySQL index (I)
- ShanDong Multi-University Training #3
- CompositionAPI 组件开发范式
- 2、MYSQL介绍
- Redis source code and design analysis -- 3 Dictionaries
- 见鬼,U盘空间怎么少了,原来是EFI分区搞的鬼,删除它
猜你喜欢

Cmake learning notes

Labview32-bit and 64 bit compatibility

Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言

滑动窗口最大值问题

Principle and simple implementation of custom MVC

Preview of authtalk phase I | comprehensive dismantling of multi tenant solutions

Redis

dba

Use tongweb's hot deployment function with caution
![[Axi] interpret the additional signals of the Axi protocol (QoS signal, region signal, and user signal)](/img/2b/15b3d831bba6aa772ad83f3ac91d23.png)
[Axi] interpret the additional signals of the Axi protocol (QoS signal, region signal, and user signal)
随机推荐
ospf-LSA
Problème de la valeur maximale de la fenêtre coulissante
ORA-00054
两种虚拟机的比较
06--- characteristics of light in media
Unity uses a map to control the transparency of the material's main map
Analysis of network communication flow of different containers on the same host
中断的分类
[flask introduction series] exception handling
优秀的jar包启动shell脚本收藏
05--- antireflective film
MySQL view
[Axi] interpret the additional signals of the Axi protocol (QoS signal, region signal, and user signal)
Authing practice | unified management solution for manufacturing identity authentication
Use of token in ogg
44、使用OrienMask进行实例分割目标检测,并进行mnn部署和ncnn部署
Tongweb production system emergency treatment plan
Explain the operation of C language file in detail
273. Grading - acwing question bank [DP]
Redis source code and design analysis -- 1 Simple dynamic string