zoukankan      html  css  js  c++  java
  • lintcode521- Remove Duplicate Numbers in Array- easy

    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:

    1. Move duplicate integers to the tail of nums => nums = [1,3,4,2,?,?].
    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.

    Challenge 
    1. Do it in O(n) time complexity.
    2. Do it in O(nlogn) time without extra space.

    1.O(n) time: 用HashSet来确认有过没,有过就swap(切记这里要做一个 i--的操作,因为你把后面没有检验过的数换到这里了,要重新确认当前位置);没有就加入set,继续。

    2.O(n) time:用HashSet。一开始就把所有数字都加入HashSet。之后遍历set(切记set没有set.get(i)方法,只能用iterator的那个冒号方法),放到数组的最前面。

    3.O(nlogn)time, but no extra space: 先sort,把一样的数字聚集到一起。再用一个int cntDistct 来同时做计数和指针,如果找到独特的数,就直接替换到前面指针的位置。

    1. HashSet+swap

    public class Solution {
        /*
         * @param nums: an array of integers
         * @return: the number of unique integers
         */
        public int deduplication(int[] nums) {
            // write your code here
            Set<Integer> set = new HashSet<Integer>();
            int countDup = 0;
            for (int i = 0; i < nums.length - countDup; i++) {
                if (!set.contains(nums[i])) {
                    set.add(nums[i]);
                } else {
                    swap(nums, i, nums.length - 1 - countDup);
                    countDup++;
                    i--;
                }
            }
            return nums.length - countDup;
        }
        
        private void swap (int[] nums, int idx1, int idx2) {
            int temp = nums[idx1];
            nums[idx1] = nums[idx2];
            nums[idx2] = temp;
        }
    }

    2. HashSet

    public class Solution {
        /*
         * @param nums: an array of integers
         * @return: the number of unique integers
         */
        public int deduplication(int[] nums) {
            // write your code here
            
            Set<Integer> set = new HashSet<Integer>();
            for (int i = 0; i < nums.length; i++) {
                set.add(nums[i]);
            }
            
            int i = 0;
            for (Integer number : set) {
                nums[i++] = number;
            }
            return set.size();
        }
    }

    3. sort

    public class Solution {
        /*
         * @param nums: an array of integers
         * @return: the number of unique integers
         */
        public int deduplication(int[] nums) {
            // write your code here
            
            Arrays.sort(nums);
            int countDis = 0;
            for (int i = 0; i < nums.length; i++) {
                if (i > 0 && nums[i] == nums[i - 1]) {
                    continue;
                }
                nums[countDis++] = nums[i];
            }
            return countDis;
        }
    }
  • 相关阅读:
    超硬核Java工程师秋招回忆录+面经汇总,为了拿BAT的研发offer我都做了那些准备?
    使用Prometheus监控Golang服务-基于YoyoGo框架
    Kubernetes Pod OOM 排查日记
    Golang语言排序的几种方式
    设计公共组件需要注意什么
    有关WebSocket必须了解的知识
    docker容器技术
    【SpringBoot】 中时间类型 序列化、反序列化、格式处理
    安装Scrapy的时候报错error: Microsoft Visual C++ 14.0 is required.
    python爬虫学习05-爬取图片
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7771613.html
Copyright © 2011-2022 走看看