26. Remove Duplicates from Sorted Array
来源:互联网 发布:block matching算法 编辑:程序博客网 时间:2024/04/30 14:38
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2]
,
Your function should return length = 2
, with the first two elements of nums being 1
and 2
respectively. It doesn't matter what you leave beyond the new length.
问题:不允许开辟额外空间,重复元素的话保留首次出现的
解决:在len的后一位i开始遇到重复,则i开始后移直到非重复的出现,将新元素放到len后面
public int removeDuplicates(int[] nums) { if(nums.length==0||nums==null) return 0;int len=0;for(int i=1;i<nums.length;i++){if(nums[i]!=nums[len]){ nums[++len]=nums[i]; continue;}}for(int i=0;i<=len;i++){System.out.print(nums[i]+"\t");}return len+1; }
0 0
- [LeetCode]26.Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- LeetCode 26. Remove Duplicates from Sorted Array
- LeetCode --- 26. Remove Duplicates from Sorted Array
- LeetCode 26.Remove Duplicates from Sorted Array
- [Leetcode] 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- 【leetcode】26. Remove Duplicates from Sorted Array
- 【leetcode】 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- [leetCode]26. Remove Duplicates from Sorted Array
- LeetCode 26.Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array LeetCode
- 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- perl encode_utf8 decode_utf8
- Java自定义长度可变数组
- Tomcat中Spring如何管理应用程序类的问题
- 软件测试面试宝典(杂乱)
- 算法与数据结构之线性结构的相关知识,简单易懂。
- 26. Remove Duplicates from Sorted Array
- Softmax&Overfitting&Regulization&Dropout
- 3.8学习内容-常用的Arrays工具类方法和冒泡排序
- Linux Kernel设备驱动模型之CPU相关初始化
- 深入理解JVM(一):JVM内存区域
- 剑指offer- hash排序
- 图像标定
- c++ string和char用法
- 变量的作用域