当前位置:网站首页>力扣刷題日記/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上宮水三葉博主的內容,如有侵權,聯系删除
边栏推荐
- Lua emmylua annotation details
- Neglected problem: test environment configuration management
- Win32 API 访问路由的加密网页
- Rainfall warning broadcast automatic data platform bwii broadcast warning monitor
- [209] go language learning ideas
- 通过事件绑定实现动画效果
- 无心剑中译伊丽莎白·毕肖普《一门技艺》
- 内核中时间相关的知识介绍
- The top half and bottom half of the interrupt are introduced and the implementation method (tasklet and work queue)
- Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
猜你喜欢
RecastNavigation 之 Recast
DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply
MySQL常用增删改查操作(CRUD)
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
爬虫初级学习
How to test MDM products
Neglected problem: test environment configuration management
删除二叉搜索树中的节点附图详解
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
I always thought that excel and PPT could only be used for making statements until I saw this set of templates (attached)
随机推荐
I wrote a learning and practice tutorial for beginners!
2022年全国CMMI认证补贴政策|昌旭咨询
Android uses sqliteopenhelper to flash back
New technology releases a small program UNIPRO to meet customers' mobile office scenarios
Face_recognition人脸识别之考勤统计
Mysql5.7 installation tutorial graphic explanation
How to test MDM products
Interview summary of large factory Daquan II
Flask lightweight web framework
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
表情包坑惨职场人
[HCIA continuous update] network management and operation and maintenance
2022 national CMMI certification subsidy policy | Changxu consulting
明星开店,退,退,退
Numpy 的仿制 2
RecastNavigation 之 Recast
Face_ Attendance statistics of recognition face recognition
General environmental instructions for the project
为啥有些线上演唱会总是怪怪的?
Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate