Remove Duplicates from Sorted Array
来源:互联网 发布:阿巴町手表软件下载 编辑:程序博客网 时间:2024/06/07 01:38
题目明确指出了对空间复杂度的限制,而且还隐含了对时间复杂度的限制。
起初写了个时间复杂度为O(n^2)的版本,提交后提示超时。
经过修改将复杂度降到了O(n)
class Solution {public: int removeDuplicates(int A[], int n) { if(n == 0 || n == 1) return n; int pre = A[0]; int count = n; for(int i = 1; i < n; i++) { if(A[i] == pre) { A[i] = A[0]; count--; } else { pre = A[i]; } } int position = 1; for(int i = 1; i < n; i++) { if(A[i] != A[0]) { A[position] = A[i]; position++; } } return count; }};
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
- zz
- POJ A New Stone Game
- GHO是什么文件 与ISO镜像文件有什么不同
- PJMEIDA 视频打开
- In-App Purchases入门
- Remove Duplicates from Sorted Array
- 安卓手机通过蓝牙与单片机通信
- UVa 11817 Tunnelling the Earth 计算球面距离
- armv6, armv7, armv7s的区别
- comet4j java服务端推送消息到web页面实例
- load-on-startup
- mysql-5.6.17-linux-glibc2.5-i686.tar.gz 32安装
- 在iOS中使用静态库
- jquery ajax超时设置