移除有序数组中的重复数字
来源:互联网 发布:protocol buffer json 编辑:程序博客网 时间:2024/05/22 05:11
描述:给定一个有序数组,移除数组中重复的数字并保证当前数字只出现一次,最后返回移除后的数组长度。
如:输入:array A={1,1,2}; 输出:length=2;A={1,2};
思路一:申请一个新数组,并从第二位数组元素开始遍历,将每个元素与加入新数组的元素进行比较,若相等则跳过否则加入新数组中。时间复杂度为线性,空间复杂度为线性;
class solution{public:int remove(int &A[],int n){if(n==0)return 0;int *B=(int *)malloc(sizeof(int)*n);*B=A[0]; int len=1; for(int i=1;i<n;i++){if(*B==A[i])continue;++*B=A[i]; ++len; } return len; }};思路二:可以不必申请空间直接在原数组上标记未重复元素的位置。时间复杂度为线性,空间复杂度为1;
class solution{public:int remove(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];}index++;return index;}};思路三:可以直接使用C++STL。时间复杂度为线性,空间复杂度为1;
#include<algorithm>class solution{public:int remove(int &A[],int n){return distance(A,unique(A,A+n));};
0 0
- 移除有序数组中的重复数字
- 移除有序数组中的重复数字2
- 移除有序数组中的重复元素
- leetcode 4. 移除有序数组中的重复元素 Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array(移除有序数组中的重复元素)
- 80.移除有序数组中重复元素第二弹
- 有序数组,插入重复数字
- 移除重复数字
- 移除数组中的重复元素
- 【计蒜课】移除数组中的重复元素
- 移除数组中的重复元素
- 计蒜客 移除数组中的重复元素
- java常用算法之返回目标数字在有序数组中的位置(假设有序数组中不存在重复数字)
- 【leetcode 移除有序序列重复数字】Remove Duplicates from Sorted Array(List) I(II)
- 数组中的重复数字
- 数组中的重复数字
- Remove Duplicates from Sorted Array--从有序数组中移除重复元素
- (有序数组中移除重复元素)Remove Duplicates from Sorted Array
- 前端技术:bootstrap(控件、插件、html\css\js开发框架)
- Xcode比较常用的快捷键
- 使用DOS命令调用py文件中的函数
- 学习c++心得和方法
- Unable to compile class for JSP org.apache.jasper.JasperException: Unable to compile class for JSP
- 移除有序数组中的重复数字
- springmvc+spring-security+mybatis +redis 框架抽取
- Jedis(Java操作Redis)入门学习总结
- 第16周项目2-用文件保存学生名单
- appium for windows 环境搭建
- 2015年Java开发岗位面试题归类
- Android App支付系列(一):微信支付接入详细指南(附官方支付demo)
- 第17周项目1—小玩文件 (2)
- 在ViewController中添加n个子ViewController