[LeetCode] Remove Duplicates from Sorted Array
2015-06-15 20:02
351 查看
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.
C语言版:
int removeDuplicates(int* nums, int numsSize) {
int count=0;
if(numsSize==0)
return 0;
int key=nums[0];
for(int i=0;i<numsSize;i++)
if(nums[i]!=key)
{
nums[count++]=key;
key=nums[i];
}
nums[count++]=key;
return count;
}
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.
C语言版:
int removeDuplicates(int* nums, int numsSize) {
int count=0;
if(numsSize==0)
return 0;
int key=nums[0];
for(int i=0;i<numsSize;i++)
if(nums[i]!=key)
{
nums[count++]=key;
key=nums[i];
}
nums[count++]=key;
return count;
}
相关文章推荐
- 在Excel里快速插入目录
- C++11.1 章 泛型算法---能够作用在不同类型的容器和元素上的算法
- Android LayoutInflater详解
- ubuntu常用命令
- Android之单元测试学习
- UIButton
- leetcode -Swap Nodes in Pairs
- [技巧篇]03.关于MyBatis的简单批量处理
- android.content.SharedPreferences.edit()
- Android之菜单总结
- Java产生随机数
- Jenkins源码分析
- jQuery实战:创建元素包装集,选择将被操作的元素
- Java集合类型(一)
- Valid Sudoku
- jenkins中使用到的Jelly标签库
- NLPIR(ICTCLAS2015)python调用接口配置
- Hadoop单机模式安装
- ubuntu开启sshd服务(转)
- poj-3253哈夫曼树