LeetCode 442. Find All Duplicates in an Array

转载请注明出处:http://www.cnblogs.com/liangyongrui/p/6353922.html

开个数组 hash的办法 大家都会。

但是这题不能用辅助空间,所以,我给这个方法起名叫别样hash

因为,所有的数字 都在[1,n] 所以可以用数字的正负来表示hash值

具体见代码。

    public List<Integer> findDuplicates(int[] nums) {
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < nums.length; ++i) {
            int index = Math.abs(nums[i]) - 1;
            if (nums[index] < 0)
                res.add(index + 1);
            else nums[index] = -nums[index];
        }
        return res;
    }
时间: 2024-12-19 02:16:05

LeetCode 442. Find All Duplicates in an Array的相关文章

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

(Java) LeetCode 442. Find All Duplicates in an Array —— 数组中重复的数据

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

442. Find All Duplicates in an Array - LeetCode

Question 442.?Find All Duplicates in an Array Solution 题目大意:在数据中找重复两次的数 思路:数组排序,前一个与后一个相同的即为要找的数 Java实现: public List<Integer> findDuplicates(int[] nums) { List<Integer> ans = new ArrayList<>(); if (nums.length == 0) return ans; Arrays.so

442. Find All Duplicates in an Array

   题目描述     442. Find All Duplicates in an Array   题目分析 遍历数组,将每次取出的值放入到正确的位置上,如nums[0]的值为4,则将4放入到nums[3]中(交换值方式). 如果正确的位置已经有正确的值(nums[index]==index+1),则不用处理. 如此,将不断地将值放入到正确的位置.而放不进去的值则为重复的值. 补充:可以使用LeetCode 448的思路,遍历数组,每取一值,index=nums[index]走一遍并一路做好标

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte

Leetcode 线性表 Remove Duplicates from Sorted Array

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array Total Accepted: 14789 Total Submissions: 46601 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new l

leetcode第一刷_Remove Duplicates from Sorted Array II

水题. 我之前说过包含至多几个至少几个的问题都比较难,这个题可是让我大脸了.至多可以重复一次,那就重复次数多于两次再计算重复,否则的话像普通的数据一样直接按照重复次数前移就可以了嘛.不过说归说,这种inspace的思想还是有些用处的,数组这种实现方式致命的缺点就是删除或者添加中间的元素代价太大,因为不好把握数据的最终位置.这个题是一种情况,合并两个排序好的数组也是一个例子. class Solution { public: int removeDuplicates(int A[], int n)

leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)

题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 说明: 1)设个标志可实现 实现: 1 class Solution { 2 public

leetcode 题解: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