当前位置:网站首页>力扣刷題日記/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上宮水三葉博主的內容,如有侵權,聯系删除
边栏推荐
- Win32 API 访问路由的加密网页
- 【Hot100】32. 最长有效括号
- Reptile elementary learning
- 为啥有些线上演唱会总是怪怪的?
- 高中物理:力、物体和平衡
- Numpy 的仿制 2
- MySQL常用增删改查操作(CRUD)
- Grain Mall (I)
- Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
- Rainfall warning broadcast automatic data platform bwii broadcast warning monitor
猜你喜欢
五千字讲清楚团队自组织建设 | Liga 妙谈
谷粒商城(一)
"In Vietnam, money is like lying on the street"
Neglected problem: test environment configuration management
Ks007 realizes personal blog system based on JSP
能源行业的数字化“新”运维
7 RSA Cryptosystem
2022年全国CMMI认证补贴政策|昌旭咨询
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)
RecastNavigation 之 Recast
随机推荐
简单易用的地图可视化
用于图数据库的开源 PostgreSQL 扩展 AGE被宣布为 Apache 软件基金会顶级项目
Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate
RecastNavigation 之 Recast
[HCIA continuous update] network management and operation and maintenance
要上市的威马,依然给不了百度信心
Pytoch deep learning environment construction
Stars open stores, return, return, return
Win32 API access route encrypted web pages
LD_ LIBRARY_ Path environment variable setting
能源行业的数字化“新”运维
Redis主从复制
android使用SQLiteOpenHelper闪退
Flask lightweight web framework
Superscalar processor design yaoyongbin Chapter 6 instruction decoding excerpt
The controversial line of energy replenishment: will fast charging lead to reunification?
Imitation of numpy 2
爬虫初级学习
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
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)