当前位置:网站首页>Array: question brushing record
Array: question brushing record
2022-07-01 07:42:00 【Abro.】
Overview of knowledge points :

1. Sum of two numbers :
Given an array of integers nums And an integer target value target, Please find... In the array And is the target value target the Two Integers , And return their array subscripts .
You can assume that each input corresponds to only one answer . however , The same element in the array cannot be repeated in the answer .
【 example 】
Input :nums = [2,7,11,15], target = 9
Output :[0,1]
explain : because nums[0] + nums[1] == 9 , return [0, 1] .
class Solution {// Double pointer violence solution
public int[] twoSum(int[] nums, int target) {
for(int i=0;i<nums.length;i++){// From 1 The number starts to traverse
for(int j=i+1;j<nums.length;j++){// Double pointer , From 2 The number starts to traverse
if(nums[i]+nums[j]==target)
return new int[] {i,j};
}
}
//throw new IllegalArgumentException("no two sum solution");
return new int[] {};
}
}2. The closest sum of three :
Give you a length of n Array of integers for nums and A target value target. Please start from nums Choose three integers , Make their sum with target Nearest .
Returns the sum of the three numbers .
It is assumed that there is only exactly one solution for each set of inputs .
【 example 】
Input :nums = [-1,2,1,-4], target = 1 Output :2 explain : And target The closest and 2 (-1 + 2 + 1 = 2) .
class Solution {// Sort the array first
public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int sum = 0;
int rand = nums[0]+nums[1]+nums[2];// The sum of the first three numbers
for(int i=0;i<nums.length;i++){// Traversal array
int start = i+1,end = nums.length-1;// Define two pointers
while(start<end){
sum = nums[i]+nums[start]+nums[end];
if(Math.abs(target-sum)<Math.abs(target-rand)) rand = sum;// Choose the one with a small absolute difference from the target value
if(sum > target) end--;// If it is greater than the target value, the end pointer moves forward
else if(sum < target) start++;// Less than the target value, the header pointer moves back
else return rand;// If it is equal to the target value, it returns rand
}
}
return rand;// Finally back to rand
}
}3. Remove duplicate items from an ordered array :
Here's an ordered array nums , Would you please In situ Delete duplicate elements , Make each element Only once , Returns the new length of the deleted array .
Don't use extra array space , You must be there. In situ Modify input array And using O(1) Complete with extra space .
【 example 1】
Input :nums = [1,1,2] Output :2, nums = [1,2] explain : Function should return the new length 2, And the original array nums The first two elements of are modified to 1, 2. You don't need to think about the elements in the array that follow the new length .
【 example 2】
Input :nums = [0,0,1,1,1,2,2,3,3,4] Output :5, nums = [0,1,2,3,4] explain : Function should return the new length 5, And the original array nums The first five elements of are modified to 0, 1, 2, 3, 4. You don't need to think about the elements in the array that follow the new length .
class Solution {
public int removeDuplicates(int[] nums) {
int j=0;// Counter
for(int i=0;i<nums.length;i++){// Traversal array
if(nums[i]!=nums[j])
nums[++j]=nums[i];
}
return j+1;
}
}4. Remove elements :
Give you an array nums And a value val, You need In situ Remove all values equal to val The elements of , And return the new length of the array after removal .
Don't use extra array space , You have to use only O(1) Additional space and In situ Modify input array .
The order of elements can be changed . You don't need to think about the elements in the array that follow the new length .
【 example 】
Input :nums = [3,2,2,3], val = 3 Output :2, nums = [2,2] explain : Function should return the new length 2, also nums The first two elements in are 2. You don't need to think about the elements in the array that follow the new length . for example , The new length returned by the function is 2 , and nums = [2,2,3,3] or nums = [2,2,0,0], It will also be seen as the right answer .
class Solution {//
public int removeElement(int[] nums, int val) {
int ans =0;// Counter
for(int num:nums){// Traversing array elements
if(num!=val){// It's not equal to val Then put num Put it in the current position
nums[ans] = num;
ans++;// Counter ++
}
}
return ans;// Return counter
}
}5. Sum of three numbers :
Give you one containing n Array of integers nums, Judge nums Are there three elements in a,b,c , bring a + b + c = 0 ? Please find out all and for 0 Triple without repetition .
Be careful : Answer cannot contain duplicate triples .
【 example 】
Input :nums = [-1,0,1,2,-1,-4] Output :[[-1,-1,2],[-1,0,1]]
class Solution {// With the article 2 Similar ideas
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for(int i=0;i<nums.length;i++){
if(nums[i]>0){
return result;
}
if(i>0&&nums[i] == nums[i-1]){
continue;
}
int left = i+1;
int right = nums.length-1;
while(left<right){
int sum = nums[i] + nums[left] + nums[right];
if(sum > 0) right--;
else if(sum < 0) left++;
else{
result.add(Arrays.asList(nums[i],nums[left],nums[right]));
while(right>left && nums[right] == nums[right-1]) right--;
while(right>left && nums[left] == nums[left+1]) left--;
right--;
left++;
}
}
}
return result;
}
}边栏推荐
- 【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字
- [target detection] yolov5, the shoulder of target detection (detailed principle + Training Guide)
- 【微服务|openfeign】Feign的日志记录
- Is it reliable to open an account on the compass with your mobile phone? Is there any potential safety hazard
- C# Newtonsoft.Json中JObject的使用
- [skill] create Bat quick open web page
- redisson看门狗机制,redisson看门狗性能问题,redisson源码解析
- 奥迪AUDI EDI 项目中供应商需要了解哪些信息?
- Redisson utilise la solution complète - redisson Documents officiels + commentaires (Partie 1)
- Redisson watchdog mechanism, redisson watchdog performance problems, redisson source code analysis
猜你喜欢

2022年茶艺师(中级)复训题库及答案

Stepsister becomes stepmother, son breaks off relationship with himself, and musk, the world's richest man, why is it so miserable?

Custom events of components ②

【无标题】

LeetCode+ 71 - 75

ctfshow-web355,356(SSRF)

kubernetes资源对象介绍及常用命令(二)

Redisson uses the complete solution - redisson official documents + Notes (Part 1)

Image style migration cyclegan principle

Jax's deep learning and scientific computing
随机推荐
【mysql学习笔记27】存储过程
Saving db4i depth camera pictures with MATLAB
[software] phantomjs screenshot
Sorting out tcp/udp communication problems
Which securities company is better or safer for mobile phone account opening
[target detection] yolov5, the shoulder of target detection (detailed principle + Training Guide)
2022 electrician (intermediate) recurrent training question bank and answers
手机开户选哪个证券公司比较好,哪个更安全
良心安利万向轮 SolidWorks模型素材网站
2022年流动式起重机司机考试练习题及在线模拟考试
2022茶艺师(初级)操作证考试题库及模拟考试
Warm congratulations on the successful listing of five elements hehe liquor
How do the top ten securities firms open accounts? In addition, is it safe to open a mobile account?
【无标题】
C# Newtonsoft.Json中JObject的使用
Download xshell and xftp
如何让两融交易更极速
go-etcd
1286_FreeRTOS的任务优先级设置实现分析
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario