当前位置:网站首页>[cute new problem solving] sum of three numbers
[cute new problem solving] sum of three numbers
2022-07-19 02:04:00 【InfoQ】
Title Description
Problem solving instructions
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
// According to the title , It's summation 0 Triple of , therefore target Pass on 0 that will do
return threeSum(nums, 0);
}
/**
* Sum of three numbers
* In the array nums Find all sums equal to target Non repeating triples of
*/
public List<List<Integer>> threeSum(int[] nums, int target) {
// First sort the array , That's the premise
Arrays.sort(nums);
// 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 = 0; 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) {
// TODO
}
}
public class Solution {
/**
* 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;
}
}
边栏推荐
- iFair: Learning Individually Fair Data Representations for Algorithmic Decision Making
- Allegro design entry CIS and OrCAD capture CIS relationship
- [literature reading] mcunet: tiny deep learning on IOT devices
- VGG (Visual Geometry Group)
- Second order edge detection Laplacian of Guassian Gaussian Laplacian operator
- Differences between saber PSPICE Simulink power supply simulation software
- HRNet
- Fairness in Deep Learning: A Computational Perspective
- VIM 配置文件
- 04基于ZigBee的室内无线定位系统设计
猜你喜欢

Fair Attribute Classification through Latent Space De-biasing

04基于ZigBee的室内无线定位系统设计

01 design of intelligent warehouse management system based on RFID

Learning Transferable Visual Models From Natural Language Supervision

如何理解Volatile以及如何使用它

Mxnet network model (V) conditional Gan neural network

二阶边缘检测 - Laplacian of Guassian 高斯拉普拉斯算子

Leveraging Semi-Supervised Learning for Fairness using Neural Networks

openGauss Developer Day 2022|东方通诚邀您莅临“东方通生态工具分论坛”

Can protocol communication
随机推荐
How to understand volatile and how to use it
【文献阅读】Counting Integer Points in Parametric Polytopes Using Barvinok‘s Rational Functions
Hands on deep learning -- linear neural network
【文献阅读】Multi-state MRAM cells for hardware neuromorphic computing
openGauss Developer Day 2022|东方通诚邀您莅临“东方通生态工具分论坛”
JS tree view array batch circular operation
One vs One Mitigation of Intersectional Bias
Switch details
Fairness in Semi-supervised Learning: Unlabeled Data Help to Reduce Discrimination
01基于RFID的智能仓储管理系统设计
Neutralizing Self-Selection Bias in Sampling for Sortition
Boost线程池
使用异或交换两个变量是低效的
Learning Transferable Visual Models From Natural Language Supervision
bais mintigation post-processing for individual and group fairness
ACE下载地址
动手学深度学习---从全连接层到卷积层篇
06基于STM32的智能电子药盒设计
Fair Multiple Decision Making Through Soft Interventions
中心极限定理