LeetCode算法题之Remove Duplicates from Sorted Array
来源:互联网 发布:慈溪市编程成绩 编辑:程序博客网 时间:2024/06/06 13:25
问题描述:
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 A = [1,1,2]
,
Your function should return length = 2
, and A is now [1,2]
.
就是去掉数组中重复的数字,只保留一个,返回处理后数组中的元素个数,要求不能再设置一个数组!
解题思路:
很简单,不多说!算法复杂度O(n)!
class Solution {public: int removeDuplicates(int A[], int n) { if(n == 0) return 0; int start = 0; int key = A[0]; int i = 0; while(i < n) { if (A[i] != key) { A[start++] = key; key = A[i]; } i++; } A[start++] = key; return start; }};
0 0
- LeetCode算法题之Remove Duplicates from Sorted Array
- leetcode之Remove Duplicates from Sorted Array
- LeetCode 之 Remove Duplicates from Sorted Array
- LeetCode之Remove Duplicates from Sorted Array
- LeetCode之Remove Duplicates from Sorted Array
- LeetCode之Remove Duplicates from Sorted Array
- LeetCode之Remove Duplicates from Sorted Array
- LeetCode之Remove Duplicates from Sorted Array
- 【LeetCode】之Remove Duplicates from Sorted Array
- leetcode之Remove Duplicates from Sorted Array
- 【Leetcode】之Remove Duplicates from Sorted Array
- leetcode之 Remove Duplicates from Sorted Array
- LeetCode 之 Remove Duplicates from Sorted Array
- LeetCode之Remove Duplicates from Sorted Array
- 【Leetcode】算法题26 Remove Duplicates from Sorted Array
- LeetCode第26题之Remove Duplicates from Sorted Array
- [leetcode][array] Remove Duplicates from Sorted Array
- 【Leetcode】【Array】Remove Duplicates from Sorted Array
- 第一章
- wxWidget文件路径处理及字符串拆分
- Linux 平台误删 home oracle 根目录的解决方法
- C++常见多线程编程
- NSLayoutConstraint使用
- LeetCode算法题之Remove Duplicates from Sorted Array
- 【转】C#中的Invoke
- 安卓学习笔记--通过ContentProvider读写系统的通讯录
- UILabel详解
- TabHost 和 FragmentTabHost面面观
- KVC&KVO
- HDOJ Bone Collector 2602
- 关于操作系统学习路线分析
- Hadoop集群和Hbase集群搭建