LeetCodeOJ--Remove Duplicates from Sorted Array(c++ && python版本)
来源:互联网 发布:手机拍照软件排行 编辑:程序博客网 时间:2024/06/03 11:21
Remove Duplicates from Sorted Array
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]
.C++版本:AC
class Solution {public: int removeDuplicates(int A[], int n) { if(A == NULL || n <=0) { return 0;} // 非STL版本 时间复杂度O(n), 空间复杂度O(1) //int index = 0; //for(int i = 1; i < n; i++) //{ // if(A[index] != A[i]) // { // A[++index] = A[i]; // } //} //return index + 1; // STL版本 时间复杂度O(n), 空间复杂度O(1) return distance(A, unique(A, A+n)); }};
2.python版本AC
class Solution: # @param a list of integers # @return an integer def removeDuplicates(self, A): L = len(A) if L <= 0: return 0 j = 0 for i in range(1,L): if A[j] != A[i]: j = j+1 A[j] = A[i] return j+1
0 0
- LeetCodeOJ--Remove Duplicates from Sorted Array(c++ && python版本)
- leetcodeOJ 80. Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array python
- Remove Duplicates from Sorted Array python 题解
- Remove Duplicates from Sorted Array [Python]
- 26. Remove Duplicates from Sorted Array (python)
- 26. Remove Duplicates from Sorted Array-Python
- Remove Duplicates from Sorted Array [python解]
- Leetcode-Remove Duplicates from Sorted Array-Python
- leetcodeOJ 83. Remove Duplicates from Sorted List
- 【Leetcode】【python】Remove Duplicates from Sorted Array/Remove Duplicates from Sorted Array II
- 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
- c/c++ 字符数组、字符指针和字符串类型
- 取消apache索引权限
- 搞的发
- SQL总结
- 【LeetCode】Pow(x, n)
- LeetCodeOJ--Remove Duplicates from Sorted Array(c++ && python版本)
- MHL与micro USB 2.0引脚对应关系
- 指针理解错误之构建链表操作
- 复选框的选择
- Turning Off & editting "What's New Window" For virtuoso
- android:windowSoftInputMode属性详解
- OCP 1Z0 052 77
- NSRunloop的理解
- 杭电 1203 I NEED A OFFER!