【Leetcode】【Easy】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].
解题思路1:
数组中相似的元素,不能像链表一样删除重构。可以使用两个index,一个用来遍历原数组,另一个用来标记新构造数组的尾部/长度。
所谓新构造数组,只是在原数组的空间上做填充,不会使用新的空间。
代码:
class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n==)
return n;
int newArrayLen = ;
for (int i=; i<n-; i++) {
if (A[i] != A[i+]) {
A[newArrayLen++] = A[i+];
}
}
return newArrayLen;
}
};
附录-
【Leetcode】【Easy】Remove Duplicates from Sorted Array的更多相关文章
- C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array
26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...
- LeetCode Array Easy 26.Remove Duplicates from Sorted Array 解答及疑惑
Description Given a sorted array nums, remove the duplicates in-place such that each element appear ...
- LeetCode Linked List Easy 83. Remove Duplicates from Sorted List
Description Given a sorted linked list, delete all duplicates such that each element appear only onc ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode】080. Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
随机推荐
- bzoj3262 陌上花开 cdq分治(入门)
题目传送门 思路:cdq分治处理偏序关系的模板题,主要就是学cdq分治吧,还在入门中. 代码其实也很好理解,记得树状数组操作的上限是 z的最大值,不是n的最大值,这个细节wa了好久. #include ...
- POJ - 1961 最小循环节
如果循环节存在那在前缀部分也肯定存在 如果循环节存在那至少是可以匹配的 而next是维护最大前缀的,意会意会 注意一定要先判整除,即使别的题目保证是存在循环的 特意画了一张灵魂草图帮助理解 #incl ...
- Highcharts的一些属性
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- AES/CBC/PKCS5Padding对称加密
package unit; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.cry ...
- datetime和date如何转json
import json from datetime import datetime, date class MyJson(json.JSONEncoder): def default(self, o) ...
- 【网络】CCNA实验一:Vlan、Trunk
要求: 1:不同楼层物理隔离,但逻辑相连 2:相同楼层物理相连,但逻辑隔离 3:主机可以动态获取IP地址 4:不同VLAN间可以进行通信 5:主机最终访问www.esafenet.com弹出" ...
- PIE SDK元素的选择和取消选择
1功能简介 在数据的查看等时候会用到元素的选择, 目前PIE SDK支持元素的选择和去取消选择功能,下面对这两种功能如何使用进行介绍. 2功能实现说明 2.1元素的选择 2.1.1 实现思路及原理说明 ...
- vue组件中camelCased (驼峰式) 命名与 kebab-case(短横线命名)
HTML 特性是不区分大小写的.所以,当使用的不是字符串模版,camelCased (驼峰式) 命名的 prop 需要转换为相对应的 kebab-case (短横线隔开式) 命名: 如果你使用字符串模 ...
- shell 进阶之匹配字符串
一,操作字符串 1,字符串长度 expr 命令取字符串函数 自带shell函数读取 2,匹配字符串开头字串的长度 !!!!!!!!!!!!$substring是正则表达式.!!!!!!!!! ...
- 路由器中带宽设置(Bandwidth) 20MHZ/40MHZ
原文是英文的,在这里:http://routerguide.net/setting-up-20-mhz-or-40-mhz-bandwidth-how-to-improve-wifi-network- ...