LeetCode之Remove Duplicates from Sorted Array
来源:互联网 发布:2016年5月非农数据 编辑:程序博客网 时间:2024/04/30 19:57
/*方法一:假定我们用一个新的数组B[]来记录结果,那么B[0]<B[1]<B[2]<......。因而我们可以采用如下办法吧:用i作为指针扫描数组A[],k作为指针扫描数组B[],当A[i]!=B[k]时,可获得B的k+1的元素值,即B[k+1]=A[i]。由于k<=i,因此直接用A数组即可记录结果。方法参考自:https://github.com/soulmachine/leetcode*/class Solution {public:int removeDuplicates(int A[], int n) {if(n <= 1) return n;int k(0);for(int i = 0; i < n; ++i){if(A[k] != A[i]){A[++k] = A[i];}}return ++k;}};
/*方法二:直接使用STL中的unique()函数和distance()函数。unigue()函数去除重复元素,将重复的元素放到数组末尾,并返回指向第一个重复元素的位置。distance()函数计算两个指针或者迭代器的距离。方法参考自:https://github.com/soulmachine/leetcode*/class Solution {public:int removeDuplicates(int A[], int n) {return distance(A, unique(A, A+n));}};
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][array] Remove Duplicates from Sorted Array
- 【Leetcode】【Array】Remove Duplicates from Sorted Array
- LeetCode之Remove Duplicates from Sorted Array II
- Leetcode之四Remove Duplicates from Sorted Array II
- LeetCode之Remove Duplicates from Sorted Array II
- python图形处理库PIL(Python Image Library)的介绍
- EventBus使用详解(一)——初步使用EventBus
- easyui datagrid如何加载通过ajax获得的json格式的数据
- Android 属性动画(Property Animation) 完全解析 (下)
- EventBus使用详解(二)——EventBus使用进阶
- LeetCode之Remove Duplicates from Sorted Array
- android之ConnectivityManager简介,网络连接状态
- jmeter测试TCP服务器/模拟发送TCP请求
- google面试准备
- qt qtcpserver编程入门
- 哈希表之bkdrhash算法解析及扩展
- 迷宫代码
- 七牛云存储用户怎样自己自己分析网络状况,并提供有效信息给七牛技术支持
- 为什么用freemarker而不用jsp