26. Remove Duplicates from Sorted Array(移除有序数组中的重复元素)
2016-10-27 21:48
232 查看
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 =
Your function should return length =
It doesn't matter what you leave beyond the new length.
public class Solution {
public int removeDuplicates(int[] nums) {
if(nums==null||nums.length==0)
return 0;
int p=0;
for(int q=0;q<nums.length;q++){
if(nums[p]!=nums[q])
nums[++p]=nums[q];
}return p+1;
}
}
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
1and
2respectively.
It doesn't matter what you leave beyond the new length.
public class Solution {
public int removeDuplicates(int[] nums) {
if(nums==null||nums.length==0)
return 0;
int p=0;
for(int q=0;q<nums.length;q++){
if(nums[p]!=nums[q])
nums[++p]=nums[q];
}return p+1;
}
}
相关文章推荐
- RemoveDuplicatesFromSortedArrayI II,移除有序数组里的重复元素以及移除数组里的某个元素
- Remove Duplicates from Sorted Array II 从有序数组里移除重复出现元素,最多重复出现2次 @LeetCode
- leetcode 4. 移除有序数组中的重复元素 Remove Duplicates from Sorted Array
- LeetCode 26. Remove Duplicates from Sorted Array(移除有序数组的重复元素)
- Remove Duplicates from Sorted Array--从有序数组中移除重复元素
- 每日一题之LeetCode移除元素 删除有序数组重复元素
- (有序数组中移除重复元素)Remove Duplicates from Sorted Array
- LeetCode-Remove Duplicates from Sorted List II-从有序链表中移除重复元素-链表操作
- 程序员面试题目总结--数组(二)【二分查找、找出给定数字出现次数、两个有序整型数组交集、找出数组中唯一的重复元素、判断数组中的数值是否连续相邻】
- 删除有序数组中重复元素的一个非常简洁的算法
- 面试编程题拾遗(03) --- 删除有序数组中的重复元素
- javascript 在数组中移除重复元素
- 基础5---移除数组中的重复元素
- 如何删除有序数组中的重复元素
- 删除有序数组中的重复元素
- Lettcode_26_Remove Duplicates from Sorted Array——移除排序数组中重复元素
- 程序员面试题目总结--数组(一)【递归求数组所有元素和、用一个for循环打印出一个二维数组、用递归判断数组是否是递增、有序数组中删除重复元素】
- 在一个有序数组中,有些元素重复出现。输入一个数值,求此值在数组中重复的次数
- Java算法题-删除有序数组中的重复元素
- ] 两个有序非重数组,如果查找数组之间的重复元素 (未完成)