80. Remove Duplicates from Sorted Array II
来源:互联网 发布:淘宝直通车添加宝贝 编辑:程序博客网 时间:2024/06/02 04:53
写在之前:
- 不会计数
- 不会continue(结束单次循环)
- 不会把谁赋给谁(对索引不清楚)
自己抠了近两个小时,还是不对,然后发起了求助,感谢你。
tell me
没有会不会,只有想不想。不会终将成为过去式。 —— RR
代码块
class Solution { public int removeDuplicates(int[] nums) { if(nums.length == 0) return 0; %空数组% int i=0; int count=1; for(int j = 1;j<nums.length;j++){ if(nums[j-1]!=nums[j]){ %j与前一个进行对比,判断情况一% nums[++i]=nums[j]; count=1; } else{ while(count<2){ %情况2,取前两个重复的% i++; count++; nums[i]=nums[j]; } %情况3,有多个重复的,跳出循环,重新计数,j++% } } return i+1; }}
分析代码
- j的索引表示来遍历整个数组,i的索引表示指向新数组。
- 本题总共分为三种情况,一是不重复,二是重复两次,三是重复多次。
- count计数,可以用简单的迭代法进行一两步,就可以知道判断条件了。(这是我的弱项)
- 记不住代码,就先背。
阅读全文
0 0
- 80. Remove Duplicates from Sorted Array II
- 80.Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 80. Remove Duplicates from Sorted Array II
- 递归计算P函数
- java初入多线程8
- 【JAVA_SE】10.1作业
- XTU 1274 Matrix(逆元,推公式)
- 文件管理与文本编辑
- 80. Remove Duplicates from Sorted Array II
- 解决VS弹出错误调试框解决办法。
- 基于Java的Spring容器配置一(AnnotationConfigApplicationContext实例化Spring容器)
- 实例分析Class字节码文件(二)
- 用proteus点亮一个灯泡
- SetInputData()与SetInput()
- MySQL配置文件my.cnf参数优化和中文详解
- Android中的回车键实现搜索
- Linux系统超级用户root密码忘记的解决办法