当前位置:网站首页>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
边栏推荐
猜你喜欢

MySQL常用增删改查操作(CRUD)

超标量处理器设计 姚永斌 第7章 寄存器重命名 摘录

曾经的“彩电大王”,退市前卖猪肉

People in the workplace with a miserable expression

为啥有些线上演唱会总是怪怪的?

一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)

With an estimated value of 90billion, the IPO of super chip is coming

Mysql5.7 installation tutorial graphic explanation

DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply

2022 national CMMI certification subsidy policy | Changxu consulting
随机推荐
Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
通过事件绑定实现动画效果
Once the "king of color TV", he sold pork before delisting
Set the transparent hidden taskbar and full screen display of the form
Thawte通配符SSL证书提供的类型有哪些
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
I always thought that excel and PPT could only be used for making statements until I saw this set of templates (attached)
Flask lightweight web framework
Weima, which is going to be listed, still can't give Baidu confidence
为啥有些线上演唱会总是怪怪的?
【211】go 处理excel的库的详细文档
[211] go handles the detailed documents of Excel library
项目通用环境使用说明
上市公司改名,科学还是玄学?
要上市的威马,依然给不了百度信心
网上开户安全吗?是真的吗?
Talk about seven ways to realize asynchronous programming
Grain Mall (I)
Clever use of curl command