zoukankan      html  css  js  c++  java
  • lintcode521

    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.
    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.
    Notice
    You don't need to keep the original order of the integers.
     
    1.用HashSet+相向双指针while循环。O(n)时间 O(n)空间。
    扫描数字如果出现过就扔到后面去,小心一下此时左指针不能往前,因为你换来了新的没check过的数字了。另外注意这写法不能用for循环来动指针!因为指针一格一格机械化到最后,你swap后的数字又会被check一遍,逻辑不对!需要while保持left<=right以确保扔过去的垃圾数字不会再被看。
     

    2.排序聚集重复数+同向双指针。O(nLogn)时间 O(1)空间
    同向快慢指针。慢指针指向左侧非重复数字的末尾,快指针不断向前扫描。当快指针发现它指向的数和慢指针不一样,就把该数丢给慢指针的后一个位置,慢指针++;否则只挪快指针。最后返回慢指针的值+1。

     
     
    1.O(n) 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
            if (nums == null) {
                return -1;
            }
            
            Set<Integer> set = new HashSet<>();
            int start = 0;
            int end = nums.length - 1;
            
            while (start <= end) {
                if (set.contains(nums[start])) {
                    int temp = nums[end];
                    nums[end] = nums[start];
                    nums[start] = temp;
                    end--;
                } else {
                    set.add(nums[start]);
                    start++;
                }
            }
            return start;
        }
    }
    2. O(nLogn)排序+同向双指针实现
     
    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 left = 0;
            int right = 0;
            
            while (right < nums.length) {
                if (nums[left] == nums[right]) {
                    right++;
                } else {
                    left++;
                    nums[left] = nums[right];
                    right++;
                }
            }
            return left + 1;
        }
    }
     
  • 相关阅读:
    poj1862---变形虫(贪心)
    poj1833---字典序算法
    poj1799---解析几何
    poj1665
    poj1663---纯粹找规律
    poj1658
    poj1657---chessboard
    poj1656---数黑格子
    poj1617---columnar encryption
    查找两个文件的相同之处,不同之处
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/9479228.html
Copyright © 2011-2022 走看看