当前位置:网站首页>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
边栏推荐
- The block:usdd has strong growth momentum
- Mysql5.7 installation tutorial graphic explanation
- Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate
- [system disk back to U disk] record the operation of system disk back to U disk
- Pytoch deep learning environment construction
- Is it safe to open an account online? is that true?
- gatling 之性能测试
- ITSS运维能力成熟度分级详解|一文搞清ITSS证书
- Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
- [211] go handles the detailed documents of Excel library
猜你喜欢
ISO27001认证办理流程及2022年补贴政策汇总
MySQL common add, delete, modify and query operations (crud)
mysql5.7安装教程图文详解
Easy to use map visualization
Imitation of numpy 2
Blue bridge: sympodial plant
90后开始攒钱植发,又一个IPO来了
MVC mode and three-tier architecture
Mysql5.7 installation tutorial graphic explanation
比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
随机推荐
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
Recast of recastnavigation
Pytorch深度学习之环境搭建
Pytoch deep learning environment construction
被忽视的问题:测试环境配置管理
怎么开户才是安全的,
[209] go language learning ideas
LD_ LIBRARY_ Path environment variable setting
Is it science or metaphysics to rename a listed company?
Win32 API access route encrypted web pages
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
Is it safe to download the mobile version of Anxin securities and open an account online
[HCIA continuous update] WAN technology
[daily question] 556 Next bigger element III
【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
Why are some online concerts always weird?
With an estimated value of 90billion, the IPO of super chip is coming
Face_recognition人脸识别之考勤统计
【每日一题】871. 最低加油次数