Remove Duplicate Numbers in Array
来源:互联网 发布:mac装双系统还是虚拟机 编辑:程序博客网 时间:2024/05/04 07:33
Given an array of integers, remove the duplicate numbers in it.
You should:
1. Do it in place in the array.
2. Move the unique numbers to the front of the array.
3. Return the total number of the unique numbers.
Notice
You don't need to keep the original order of the integers.
Example
Given nums = [1,3,1,4,4,2]
, you should:
- Move duplicate integers to the tail of nums => nums =
[1,3,4,2,?,?]
. - Return the number of unique integers in nums =>
4
.
Actually we don't care about what you place in ?
, we only care about the part which has no duplicate integers.
一种非常常用的算法,需要非常短的时间内一次性的写出完整的代码
java
public class Solution { /* * @param nums: an array of integers * @return: the number of unique integers */ public int deduplication(int[] nums) { // write your code here if (nums == null || nums.length == 0) { return 0; } Arrays.sort(nums); int slow = 0; int fast = 0; while (fast < nums.length) { if (nums[slow] != nums[fast]) { nums[++slow] = nums[fast++]; } else{ fast++; } } return slow + 1; }}
python
class Solution: """ @param: nums: an array of integers @return: the number of unique integers """ def deduplication(self, nums): # write your code here if nums is None or len(nums) == 0: return 0 slow, fast = 0, 0 nums.sort() while fast < len(nums): if nums[slow] != nums[fast]: slow += 1 nums[slow] = nums[fast] fast += 1 return slow + 1
阅读全文
0 0
- Remove Duplicate Numbers in Array
- Remove duplicate elements in an array
- No. 32 - Remove Numbers in Array
- Facebook面试题 Remove duplicates from array where all duplicate numbers are located adjacently
- js-remove duplicate array element
- jquery array remove duplicate records
- leetcode之 Remove Duplicate from Sorted Array
- remove duplicate from sort array 2
- LeetCode 26 remove Duplicate from sorted array
- Remove duplicate in sorted linked list 1
- count duplicate in a sorted array
- Sort in Array with duplicate keys
- Algorithm: efficient way to remove duplicate integers from an array
- Remove Duplicate
- remove duplicated value in an array
- [Leetcode] Remove Duplicates in Sorted Array
- Remove duplicates in a sorted array
- Grouping consecutive numbers in an array
- 根文件系统制作时的错误总结
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(四)
- 基于springCloud的分布式架构体系
- 少年郎,你需要封装好的ViewPager工具类
- Spring小结
- Remove Duplicate Numbers in Array
- $.ajax和java后台实现用户登录的功能
- Beginning iPhone Development with Swift 4, 4th Edition.pdf 英文原版 免费下载
- DAY 12
- Lesson5 接口
- HYSBZ4012-开店
- Linux帮助命令
- 前端新手使用karma+mocha+chai+sinon 进行angularjs 单元测试
- UVa 220 (implement)