当前位置:网站首页>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;
}
}边栏推荐
- 浏览器本地存储
- Minecraft 1.16.5模组开发(五十一) 方块实体 (Tile Entity)
- [programming training 2] sorting subsequence + inverted string
- Will Internet talents be scarce in the future? Which technology directions are popular?
- 2022年流动式起重机司机考试练习题及在线模拟考试
- 【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案
- C # read and write customized config file
- Illusory and simple screen raindrop post-processing effect
- Huawei modelarts training alexnet model
- TCP/UDP 通信问题整理
猜你喜欢

论文学习——水文时间序列相似性查询的分析与研究

What information does the supplier need to know about Audi EDI project?

2022 electrician (intermediate) recurrent training question bank and answers

How to create an exclusive vs Code theme

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

Jax's deep learning and scientific computing
![C language implementation [Sanzi chess game] (step analysis and implementation source code)](/img/3b/d32b46292ed20f31a6e1db97349df1.png)
C language implementation [Sanzi chess game] (step analysis and implementation source code)

Discussion on several research hotspots of cvpr2022

Redisson utilise la solution complète - redisson Documents officiels + commentaires (Partie 1)

base64
随机推荐
AUTOSAR learning record (1) – ECUM_ Init
良心安利万向轮 SolidWorks模型素材网站
Summary of the concept and advantages of 5g massive MIMO
Cadence OrCAD Capture “网络名”相同,但是未连接或连接错误的解放方案之nodename的用法
2022年流动式起重机司机考试练习题及在线模拟考试
weback5基础配置详解
Do securities account opening affect the security of account opening
The database is locked. Is there a solution
The triode is a great invention
Illusory and simple screen raindrop post-processing effect
【mysql学习笔记26】视图
Ctfhub port scan (SSRF)
[chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched
Eigen matrix operation Library
Alibaba OSS postman invalid according to policy: policy condition failed: ["starts with", "key", "test/"]
Missing API interface actual development series (14): ID card real name authentication verification
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario
2022广东省安全员A证第三批(主要负责人)特种作业证考试题库模拟考试平台操作
Software testing methods and techniques - overview of basic knowledge
ctfshow-web351(SSRF)