当前位置:网站首页>Li Kou brush question diary /day6/6.28
Li Kou brush question diary /day6/6.28
2022-07-04 18:23:00 【Bobo toilet cleaning spirit】
Novice Village
Delete duplicate elements in an ordered array , Double finger needling
This method has two requirements , Modify the original array , Do not use other arrays
If the length of the array is 0;
Output directly
If the length of the array is not 0;
Set two pointers ,fast and slow,fast and slow The initial value for the 1, Because to compare fast and fast-1 The elements of
fast Used to traverse all array element subscripts ,slow Used to record the number of unique elements
When fast-1 and fast The elements pointed to are different , take fast The elements represented are given to slow Represents the elements of , And will ++slow,++fast
When fast and fast-1 Point to the same element ,++fast,slow unchanged
Such as below , When the cycle starts for the first time ,fast-=1, Represents the elements of 0, and fast-1 The elements represented are the same , here ,fast Keep going right ,slow unchanged .
fast=2 when , At this time, the element pointed to is 1, and fast-1 Pointing elements 0 Different , At this time will be fast Pointing elements 1 Assign to slow Pointing elements ,nums[slow]=nums[fast], And will slow Move to the right ,++slow,fast Keep going right ,++fast
Keep going ......
until fast=9 when (n=10,fast<n), take nums[9] The value is assigned to nums[4],++slow=5, At this time will be nums[slow] Output , Is an array that eliminates duplicate elements .
slow Is the length of the new array , It is modified on the basis of the original array .
Example
Their thinking : Above
class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
if (n == 0) { // Determine whether the length of the array is 0
return 0;
}
int fast = 1, slow = 1; // Set two speed pointers
while (fast < n) { // The length of the array is n, Then the position of the element at the end is n-1
if (nums[fast] != nums[fast - 1]) {
nums[slow] = nums[fast];
++slow; // Finally, output the length of the non repeating array , So choose ++slow
}
++fast; // Prevent illegal entry into the cycle
}
return slow; // Returns the length of a non repeating array
}
}
Other ideas :
foreach Traverse (for every last )
jdk5.0 New characteristics , new for loop
for(type x:type Y)
Traverse Array or collection Y The elements of , Each iteration assigns the element value to x
for example
for(int num:nums)
Is to put nums This array is traversed , How many numbers does it have , Just traverse how many times .
When traversing, one of the values is given to num;
foreach have access to for Statement substitution
for(int i =0;i<nums.length;i++){
System.out.print(nums[i]+" ");
}
General idea :
class Solution {
public int removeDuplicates(int[] nums) {
return process(nums, 1);
}
int process(int[] nums, int k) {
int idx = 0;
for (int x : nums) {
if (idx < k || nums[idx - k] != x) nums[idx++] = x;
}
return idx;
}
}
The last two ideas quote leetcode Content of shanggongshui Sanye blogger , If there is any infringement , Contact deletion
边栏推荐
- RecastNavigation 之 Recast
- Is it safe to open an account online? is that true?
- Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)
- Is it safe to download the mobile version of Anxin securities and open an account online
- Summary of subsidy policies across the country for dcmm certification in 2022
- 通过事件绑定实现动画效果
- 高中物理:力、物体和平衡
- Grain Mall (I)
- DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
- [cloud native] what is the "grid" of service grid?
猜你喜欢
被忽视的问题:测试环境配置管理
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
I wrote a learning and practice tutorial for beginners!
7 RSA Cryptosystem
DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
Superscalar processor design yaoyongbin Chapter 6 instruction decoding excerpt
五千字讲清楚团队自组织建设 | Liga 妙谈
随机推荐
Is it science or metaphysics to rename a listed company?
You should know something about ci/cd
MySQL常用增删改查操作(CRUD)
Neglected problem: test environment configuration management
Android uses sqliteopenhelper to flash back
表情包坑惨职场人
[proteus simulation] printf debugging output example based on VSM serial port
Face_recognition人脸识别之考勤统计
【211】go 处理excel的库的详细文档
曾经的“彩电大王”,退市前卖猪肉
如何进行MDM的产品测试
[210] usage of PHP delimiter
Pytorch深度学习之环境搭建
DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply
Interview summary of large factory Daquan II
New technology releases a small program UNIPRO to meet customers' mobile office scenarios
比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
【Hot100】31. 下一个排列