当前位置:网站首页>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)
- [proteus simulation] printf debugging output example based on VSM serial port
- 中断的顶半部和底半部介绍以及实现方式(tasklet 和 工作队列)
- [test development] software testing - Basics
- Heartless sword Chinese translation of Elizabeth Bishop's a skill
- Win32 API 访问路由的加密网页
- 90后开始攒钱植发,又一个IPO来了
- 12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
- 估值900亿,超级芯片IPO来了
- Is it safe to open an account online? is that true?
猜你喜欢

Easy to use map visualization

同事悄悄告诉我,飞书通知还能这样玩

Ks007 realizes personal blog system based on JSP

Blue bridge: sympodial plant
![[cloud native] what is the](/img/00/0cb0f38bf3eb5dad02b3bc4ead36ba.jpg)
[cloud native] what is the "grid" of service grid?

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

Talk about seven ways to realize asynchronous programming

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

How to test MDM products

庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
随机推荐
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
【210】PHP 定界符的用法
[system analyst's road] Chapter 7 double disk system design (structured development method)
[211] go handles the detailed documents of Excel library
[test development] software testing - Basics
网上开户安全吗?是真的吗?
DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply
使用3DMAX制作一枚手雷
Is it safe to open an account online? is that true?
【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
【每日一题】871. 最低加油次数
2022 national CMMI certification subsidy policy | Changxu consulting
Once the "king of color TV", he sold pork before delisting
Win32 API 访问路由的加密网页
Introduction of time related knowledge in kernel
Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction
LD_ LIBRARY_ Path environment variable setting
Android uses sqliteopenhelper to flash back
Numpy 的仿制 2
华为云ModelArts的使用教程(附详细图解)