当前位置:网站首页>力扣刷題日記/day6/6.28
力扣刷題日記/day6/6.28
2022-07-04 18:22:00 【bobo潔廁靈】
新手村
删除有序數組中的重複元素,雙指針法
此方法有兩個要求,要在原數組的基礎上進行修改,不動用其它數組
如果數組的長度為0;
直接輸出即可
如果數組的長度不為0;
設置兩個指針,fast和slow,fast和slow的初值為1,因為要比較fast和fast-1的元素
fast用來遍曆所有的數組元素下標,slow用來記錄唯一的元素數目
當fast-1和fast指向的元素不相同時,將fast代錶的元素給到slow代錶的元素,並將++slow,++fast
當fast和fast-1指向的元素相同時,++fast,slow不變
例如下圖,當第一次開始循環時,fast-=1,代錶的元素0,和fast-1代錶的元素相同,此時,fast繼續往右前進,slow不變。
fast=2時,此時指向的元素為1,和fast-1指向的元素0不同,此時將fast指向的元素1賦給slow指向的元素,nums[slow]=nums[fast],並將slow往右移動,++slow,fast繼續往右前進,++fast
繼續前進下去。。。。。。
直到fast=9時(n=10,fast<n),將nums[9]的值賦給nums[4],++slow=5,此時將nums[slow]輸出,就是消除了重複元素的數組。
slow就是新的數組的長度,是在原數組的基礎上進行修改的。
例題
解題思路:如上
class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
if (n == 0) { //判斷數組的長度是否為0
return 0;
}
int fast = 1, slow = 1; //設置兩個快慢指針
while (fast < n) { //數組的長度為n,那麼末尾的元素比特置是n-1
if (nums[fast] != nums[fast - 1]) {
nums[slow] = nums[fast];
++slow; //最後要輸出無重複數組的長度,所以選擇++slow
}
++fast; //防止非法進入循環
}
return slow; //返回無重複數組的長度
}
}
其餘思路:
foreach遍曆 (for 每一個)
jdk5.0新特性,新的for循環
for(type x:type Y)
遍曆數組或集合Y的元素,每一次遍曆把元素值賦給x
例如
for(int num:nums)
就是把nums這個數組進行遍曆,它有多少個數,就遍曆多少遍。
遍曆的時候每次就把其中的一個值給num;
foreach可以使用for語句替代
for(int i =0;i<nums.length;i++){
System.out.print(nums[i]+" ");
}
通用思路:
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;
}
}
最後兩種思路引用了leetcode上宮水三葉博主的內容,如有侵權,聯系删除
边栏推荐
- Pytorch深度学习之环境搭建
- The block:usdd has strong growth momentum
- 怎么开户才是安全的,
- Imitation of numpy 2
- You should know something about ci/cd
- [daily question] 556 Next bigger element III
- Interview summary of large factory Daquan II
- ITSS运维能力成熟度分级详解|一文搞清ITSS证书
- About the pit of firewall opening 8848 when Nacos is started
- 为啥有些线上演唱会总是怪怪的?
猜你喜欢
Easy to use map visualization
股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
How to improve development quality
Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation
What if Kaili can't input Chinese???
【Hot100】32. 最长有效括号
超标量处理器设计 姚永斌 第6章 指令解码 摘录
Recast of recastnavigation
uni-app与uviewUI实现仿小米商城app(附源码)
I wrote a learning and practice tutorial for beginners!
随机推荐
[HCIA continuous update] network management and operation and maintenance
Stars open stores, return, return, return
Face_ Attendance statistics of recognition face recognition
谷粒商城(一)
俄罗斯 Arenadata 发布基于PostgreSQL的产品
[cloud native] what is the "grid" of service grid?
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
华为云ModelArts的使用教程(附详细图解)
Russia arena data releases PostgreSQL based products
fopen、fread、fwrite、fseek 的文件处理示例
2022年DCMM认证全国各地补贴政策汇总
Set the transparent hidden taskbar and full screen display of the form
Detectron2 installation method
【210】PHP 定界符的用法
Recast of recastnavigation
“在越南,钱就像躺在街上”
What if Kaili can't input Chinese???
[210] usage of PHP delimiter
如何进行MDM的产品测试
【Hot100】32. 最长有效括号