Remove duplicates from sorted array I
来源:互联网 发布:淘宝服装货源批发 编辑:程序博客网 时间:2024/06/05 04:00
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. 找duplicate的话首先想到要用hashmap,但是此题是sorted array!! 所以可以利用index来整
2. Array的remove,把要删除的元素移后/需要的元素前移,此题用的是后者,所以遇到重复元素时无需action
public class Solution { public int removeDuplicates(int[] A) { if (A == null || A.length < 1) return 0; int size = 0; for (int i = 0; i < A.length; i++) { if (A[size] != A[i]){ A[++size] = A[i]; } } return size + 1; }}
0 0
- Remove duplicates from sorted array I
- [LeetCode] Remove Duplicates From Sorted Array I
- Remove Duplicates from Sorted Array I, II
- 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
- 黑马程序员——类与对象
- CUICatalog: Invalid asset name supplied: (null), or invalid scale factor: 2.000000
- 两分钟彻底让你明白Android Activity生命周期(图文)!
- 关于typedef的用法总结
- Android开发之触摸事件处理机制详解
- Remove duplicates from sorted array I
- 关于android上的定位
- 如何查看apk需要支持的Android版本
- “占中”激起民怨 香港旅行社和餐厅向发起人索赔
- hibernate的事务和spring事务的区别
- Linux关于snmp的防火墙设置
- Java 中 Vector、ArrayList、List 使用深入剖析(转)
- 实现BASE64加密、解密算法
- c++和c的注意点和不同点: