[leetcode-26]Remove Duplicates from Sorted Array(C)
2015-07-28 12:36
501 查看
问题描述:
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.
代码如下:20ms
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.
代码如下:20ms
[code]int removeDuplicates(int* nums, int numsSize) { int num = 0; int i; for(i = 0;i<numsSize;i++){ while(i>0&&i<numsSize&&nums[i]==nums[i-1])i++; if(i<numsSize){ nums[num++] = nums[i]; } } return num; }
相关文章推荐
- Docker支持集群分布式应用意义很重大
- 在编译过程中出现未定义符号处理方法
- python3入门之字符串
- 载入动画
- Java时间差及时间比较
- MFC创建线程函数及参数的使用
- MFC如何设置拖动对话框
- HTML5 QQ登录背景动态图片
- Apache并发请求数及其TCP连接状态
- windows 无法修改默认打开方式
- 关于Servo项目中Rust代码行数的数据来源
- 【C/C++】计时函数比较
- HUD 1002 A + B Problem II
- 对kmp算法的一个小小的总结
- 学习spring事务是看的几篇文章,记一下
- unix域套接字进程间传递文件描述符
- .htaccess简介
- [笔记][思维导图]读深入理解JAVA内存模型整理的思维导图
- SAP上线前准备工作
- 1507