[LeetCode]26.Remove Duplicates from Sorted Array
来源:互联网 发布:北宋出相 知乎 编辑:程序博客网 时间:2024/05/17 06:22
Remove Duplicates from Sorted Array
Total Accepted: 6585 Total Submissions: 19851My SubmissionsGiven 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 A = [1,1,2]
,
Your function should return length = 2
, and A is now [1,2]
.
【解析】
每次和已经记录的最后一个数组元素比较,如果重复就跳过,否则将其加入数组末尾。
【代码】
/********************************** 日期:2014-01-14* 作者:SJF0115* 题目: 26.Remove Duplicates from Sorted Array* 来源:http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/* 结果:AC* 来源:LeetCode* 总结:**********************************/#include <iostream>#include <stdio.h>using namespace std;// 时间复杂度 O(n),空间复杂度 O(1)class Solution {public: //A数组的重要特点是已经排序 int removeDuplicates(int A[], int n) { if(n == 0){ return 0; } //新数组下标 int index = 0; for(int i = 1;i < n;i++){ if(A[i] != A[index]){ A[++index] = A[i]; } } return index+1; }};int main() { int result; Solution solution; int A[] = {1,1,2,2,2,2,4,4,5,6,8}; result = solution.removeDuplicates(A,11); printf("Length:%d\n",result); for(int i = 0;i < result;i++){ printf("%d ",A[i]); } return 0;}
3 0
- [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
- [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
- LeetCode 26.Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array LeetCode
- 【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
- LeetCode 26. Remove Duplicates from Sorted Array
- LeetCode - 26. Remove Duplicates from Sorted Array
- leetcode-26. Remove Duplicates from Sorted Array
- 26.[Leetcode]Remove Duplicates from Sorted Array
- HP GDCC IT 评论(comments)
- jsonObject对象与本地自定义对象相互转化
- 值得一看的英国曼彻斯特大学某技术博客
- Html5无刷新修改browser Url
- Makefile 的多行注释
- [LeetCode]26.Remove Duplicates from Sorted Array
- 关于程序员的作息安排(个人拙见)
- UVa11344 - The Huge One
- 快速积累高质量的人脉:互相推荐合适的朋友
- 使用ScheduledExecutorService接口实现Web轮询项目
- 生成自定义区间随机整数
- MSDN2012离线版
- Linux设备驱动之I2C架构分析
- JAVA获取图片大小和尺寸