Q5: 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].
解决原理:
(若输入数组无序,则先对数组进行排序,则相同值相邻)
两个游标len,i
0~len是元素不重复的子数组,即len是目标子数组的最后一个元素的索引
游标i遍历数组
若A[i]!=A[len],则为目标子数组增添一个元素
代码:
class Solution {
public:
int removeDuplicates(int A[], int n) {
int len = ;
if(n == ) return ;
//sort(A,A+n);
for(int i = ; i < n; i++){
if(A[i] != A[len]){
len++;
A[len] = A[i];
}
}
return len+;
}
};
Q5: Remove Duplicates from Sorted Array的更多相关文章
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
随机推荐
- Python运算符与表达式
Python运算符包括赋值运算符.算术运算符.关系运算符.逻辑运算符.位运算符.成员运算符和身份运算符. 表达式是将不同类型的数据(常亮.变量.函数)用运算符按照一定得规则连接起来的式子. 算术运算符 ...
- APP推广入门之AppStore数据分析
AppStore中有很多数据,但对于一个App推广者而言,最需要关注的无非这么几个,即下载量.评论.权重.榜单排名.搜索排名以及热词搜索等.至于这些数据具体代表着什么,下面就先由下载量开始,来简单介绍 ...
- Android pix转换为sp
/** * 把pix值转换为sp * * @return */ public static float px2sp(Context context, float pixValue) { final f ...
- PHP extract() 函数
PHP extract() 函数从数组中把变量导入到当前的符号表中. 对于数组中的每个元素,键名用于变量名,键值用于变量值. 第二个参数 type 用于指定当某个变量已经存在,而数组中又有同名元素时, ...
- PHP 防范IP攻击
<?php //查询禁止IP $ip =$_SERVER['REMOTE_ADDR']; $fileht=".htaccess2"; if(!file_exists($fil ...
- 使用Inno Setup 打包.NET程序,并自动安装.Net Framework
使用Inno Setup 打包.NET程序,并自动安装.Net Framework http://www.cnblogs.com/xiaogangqq123/archive/2012/03/19/24 ...
- SQL备份还原,分离附加
备份.还原.分离.附加 备份:在要备份的数据库上右键点击任务,在选择备份.在打卡的对话框中根据需要选择.注意:备份过期时间不能为0,否则会马上过期.目标可根据需要放在任何位置.最后,点击确定,备份成功 ...
- Java自带的keytool命令
使用Java自带的keytool命令,在命令行生成. 1.生成服务器端私钥kserver.keystore文件 keytool -genkey -alias serverkey -validity 1 ...
- dede取子栏目时重复显示同级栏目的终极解决方法
使用channelartlist标签时,当栏目没有子栏目是,会出现重复同级栏目的问题,解决方法如下: 先看下面的代码{dede:channelartlist typeid='2'} {dede:ty ...
- 利用freemarker生成xml
package com.mooc.freemarkerXML; import java.io.IOException; import java.io.StringWriter; import java ...