Remove Duplicates from Sorted Array
来源:互联网 发布:js遍历map集合 编辑:程序博客网 时间:2024/05/07 19:38
描述
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 arrayA = [1,1,2],
Your function should return length = 2, and A is now[1,2].
分析
无代码
<span style="font-size:14px;">#include <iostream>#include <list>#include <algorithm>#include <iterator>#include <functional>using namespace std;class Solution{public:// 使用 STL,时间复杂度 O(n),空间复杂度 O(1)static int removeDuplicatesBySTL(int A[], int n){return distance(A, unique(A, A + n));}//遍历解法//时间复杂度 O(n),空间复杂度 O(1)static int removeDuplicatesByScan(int A[], int n){if (n == 0) return 0;int index = 0;for (int i = 1; i < n; i++) {if (A[index] != A[i])A[++index] = A[i];}return index + 1;}};int main(){int A[] = {1,1,2};cout << "Length:" << Solution::removeDuplicatesBySTL(A, 3) << endl;cout << "Length:" << Solution::removeDuplicatesByScan(A, 3) << endl;return 0;}</span>
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
- 边缘检测
- 跟siki老师学C#第五天
- IOS 字符串高度
- 小米2/2S刷基于Android 5.1.1的CM12.1系统完整教程
- Fragment+viewpager+Fragment 嵌套 第二次进入该fragment不显示数据
- Remove Duplicates from Sorted Array
- LeetCode_OJ【48】Rotate Image
- Qt数据库处理时候注意的小结1
- HTML5基本标签的使用
- Python3.5安装cx_Oracle (简单学习记录)
- 弱智C的错误记录
- Android写SD卡的坑
- View的事件体系(下)(事件分发,滑动冲突)
- adb手机重启命令