当前位置:网站首页>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
边栏推荐
- Reptile elementary learning
- Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate
- android使用SQLiteOpenHelper闪退
- 78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
- I2C子系统之适配器的设备接口分析(i2c-dev.c文件分析)
- Achieve animation effect through event binding
- Win32 API 访问路由的加密网页
- Initial experience of domestic database tidb: simple and easy to use, quick to start
- Android uses sqliteopenhelper to flash back
- “在越南,钱就像躺在街上”
猜你喜欢
![[cloud native] what is the](/img/00/0cb0f38bf3eb5dad02b3bc4ead36ba.jpg)
[cloud native] what is the "grid" of service grid?

Unity makes revolving door, sliding door, cabinet door drawer, click the effect of automatic door opening and closing, and automatically play the sound effect (with editor extension code)

Rainfall warning broadcast automatic data platform bwii broadcast warning monitor

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

要上市的威马,依然给不了百度信心

估值900亿,超级芯片IPO来了

表情包坑惨职场人

Blue bridge: sympodial plant

How to improve development quality

上市公司改名,科学还是玄学?
随机推荐
线上MySQL的自增id用尽怎么办?
Flask lightweight web framework
[211] go handles the detailed documents of Excel library
SIGMOD’22 HiEngine论文解读
Implementation of shell script replacement function
【Proteus仿真】基于VSM 串口printf调试输出示例
Achieve animation effect through event binding
如何提高开发质量
2022年全国CMMI认证补贴政策|昌旭咨询
7 RSA Cryptosystem
输入的查询SQL语句,是如何执行的?
Is it safe to download the mobile version of Anxin securities and open an account online
估值900亿,超级芯片IPO来了
ITSS运维能力成熟度分级详解|一文搞清ITSS证书
“在越南,钱就像躺在街上”
I2C子系统之适配器的设备接口分析(i2c-dev.c文件分析)
[HCIA continuous update] WAN technology
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
android使用SQLiteOpenHelper闪退
Set the transparent hidden taskbar and full screen display of the form