当前位置:网站首页>力扣刷题日记/day6/6.28
力扣刷题日记/day6/6.28
2022-07-04 16:33: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上宫水三叶博主的内容,如有侵权,联系删除
边栏推荐
- celebrate! Kelan sundb and Zhongchuang software complete the compatibility adaptation of seven products
- 表情包坑惨职场人
- Is it science or metaphysics to rename a listed company?
- Analysis of I2C adapter driver of s5pv210 chip (i2c-s3c2410. C)
- Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions
- Load test practice of pingcode performance test
- Is it safe to open an account online? is that true?
- [211] go handles the detailed documents of Excel library
- 通过事件绑定实现动画效果
- "In Vietnam, money is like lying on the street"
猜你喜欢

Solve the El input input box For number number input problem, this method can also be used to replace the problem of removing the arrow after type= "number"

What if Kaili can't input Chinese???

MVC mode and three-tier architecture

Mathematical analysis_ Notes_ Chapter 7: differential calculus of multivariate functions

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)

Neglected problem: test environment configuration management

uni-app与uviewUI实现仿小米商城app(附源码)

Once the "king of color TV", he sold pork before delisting

2022 national CMMI certification subsidy policy | Changxu consulting

Weima, which is going to be listed, still can't give Baidu confidence
随机推荐
Heartless sword Chinese translation of Elizabeth Bishop's a skill
The top half and bottom half of the interrupt are introduced and the implementation method (tasklet and work queue)
LD_LIBRARY_PATH 环境变量设置
【系统分析师之路】第七章 复盘系统设计(结构化开发方法)
上市公司改名,科学还是玄学?
General environmental instructions for the project
[HCIA continuous update] WAN technology
Lua EmmyLua 注解详解
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)
【Hot100】31. 下一个排列
Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction
Pytorch深度学习之环境搭建
[210] usage of PHP delimiter
ISO27001认证办理流程及2022年补贴政策汇总
TCP waves twice, have you seen it? What about four handshakes?
要上市的威马,依然给不了百度信心
无心剑中译伊丽莎白·毕肖普《一门技艺》
如何进行MDM的产品测试
Mysql5.7 installation tutorial graphic explanation
Redis master-slave replication