remove duplicates from sorted Array
来源:互联网 发布:p身份证的软件 编辑:程序博客网 时间:2024/06/01 07:21
Given: a sorted array,
to do: remove the duplicates in place such that each element appear only once
and return the new length
requirment: do not allocate extra space for another array, you must do this in
place with constant memory.
e.g. input array nums=[1,1,2]
output 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.
26. Remove Duplicates from Sorted Array
Java code:
public class Solution {
public int removeDuplicates(int[] nums) {
if(nums==null||nums.length==0)return 0;
int index = 0;
int i;
for (i=1; i<nums.length; i++)
{
if(nums[index]!=nums[i]){
nums[++index]=nums[i];
}
}
return index+1;
}
}
another solution
public class Solution{
public int removeDuplicates(int[] nums){
if(nums==null || nums.length==0)
return 0;
Integer cur = null;//special value, means uninitialized.
int len = 0;
for(int n : nums){
if(cur == null || cur < n){
cur = n;
nums[len++] = n;
}
}
return len;
}
}
to do: remove the duplicates in place such that each element appear only once
and return the new length
requirment: do not allocate extra space for another array, you must do this in
place with constant memory.
e.g. input array nums=[1,1,2]
output 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.
26. Remove Duplicates from Sorted Array
Java code:
public class Solution {
public int removeDuplicates(int[] nums) {
if(nums==null||nums.length==0)return 0;
int index = 0;
int i;
for (i=1; i<nums.length; i++)
{
if(nums[index]!=nums[i]){
nums[++index]=nums[i];
}
}
return index+1;
}
}
another solution
public class Solution{
public int removeDuplicates(int[] nums){
if(nums==null || nums.length==0)
return 0;
Integer cur = null;//special value, means uninitialized.
int len = 0;
for(int n : nums){
if(cur == null || cur < n){
cur = n;
nums[len++] = n;
}
}
return len;
}
}
0 0
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates From Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- 标准IO与文件IO
- 学习网站
- Codeforces Round #333 (Div. 2) A. Two Bases (进制转换比较大小)
- 外语-服务-钱-见识
- Git 学习(1)
- remove duplicates from sorted Array
- Android中的文件上传下载
- UIControl类控件(一)
- 几何图形案例(openGL)
- 实战c++中的智能指针unique_ptr系列--通过unique_ptr对shared_ptr进行初始化
- HDU 1205 吃糖果(鸽巢原理)
- 堆排序的java简单实现以及讲解
- IO_字符流_纯文本_节点流_读取_写出_拷贝JAVA151
- C++整行读取.txt文件