https://leetcode.com/problems/find-all-duplicates-in-an-array/

典型的数组中的重复数。这次是通过跳转法,一个个跳转排查的。因为查过的不会重复处理,所以复杂度也是O(n)。

后面发现了别人一个更好的做法。。。如下:

public class Solution {
// when find a number i, flip the number at position i-1 to negative.
// if the number at position i-1 is already negative, i is the number that occurs twice. 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(Math.abs(index+1));
nums[index] = -nums[index];
}
return res;
}
}

我的做法:

package com.company;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; class Solution {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> list = new ArrayList<>();
int index = 1;
while (index <= nums.length) {
int next = nums[index-1];
nums[index-1] = -1;
while (next != -1 && next != index && -1 != nums[next-1] && next != nums[next-1]) {
int tmp = nums[next-1];
nums[next-1] = next;
next = tmp;
} if (next == -1) {
}
if (next == index) {
nums[index-1] = next;
}
else if (-1 == nums[next-1]) {
nums[next-1] = next;
}
else {
list.add(next); }
index++;
}
return list;
} } public class Main { public static void main(String[] args) {
System.out.println("Hello!");
Solution solution = new Solution(); int[] nums = {};
List<Integer> ret = solution.findDuplicates(nums);
System.out.printf("ret len is %d\n", ret.size());
Iterator iter = ret.iterator();
while (iter.hasNext()) {
System.out.printf("%d,", iter.next());
}
System.out.println(); }
}

find-all-duplicates-in-an-array(典型的数组中的重复数,不错,我做出来了,可是发现别人有更好的做法)的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  2. 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 ...

  3. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  4. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  5. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  6. [LeetCode] 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 ...

  7. lintcode :Remove Duplicates from Sorted Array 删除排序数组中的重复数字

    题目: 删除排序数组中的重复数字 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成.  样例 ...

  8. Leetcode26--->Remove Duplicates from Sorted Array(从排序数组中移除相同的元素)

    题目: 给定一个排序数组,移除重复出现的元素,保证每个元素最终在数组中只出现一次.返回新数组的长度length; 要求:不能分配额外的一个数组使用,必须使用原地排序的思想,且空间复杂度为O(1) 举例 ...

  9. 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)

      Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...

随机推荐

  1. Hadoop启动异常情况解决方案

    1. 启动时报WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using b ...

  2. SVN服务器搭建和使用

    SVN服务器搭建和使用 Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下 ...

  3. Careercup - Google面试题 - 5735304249999360

    2014-05-03 23:18 题目链接 原题: Insert a element in a sorted circular linked list 题目:题意简单明了,向一个有序的循环单向链表中插 ...

  4. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  5. Android journey 2 @Android系统框架

    此刻,本人还是一个android菜鸟,刚刚开始起步学习android相关知识,想用blog记录自己学习的过程,一方面给他人提供帮助,另一方面给自己个复习的地方. 在一起学习Android之前,先跟大家 ...

  6. 1562: [NOI2009]变换序列 - BZOJ

    Description Input Output Sample Input 5 1 1 2 2 1 Sample Output 1 2 4 0 3 HINT 30%的数据中N≤50:60%的数据中N≤ ...

  7. Wmware Center安装过程记录

    前几天在服务器中安装了Vmware Esxi,以及在客户端中安装了Vmware Client,因此希望能够装一下Vmware vCenter Server试验一下,结果并不顺利,在此记录.   从官网 ...

  8. Linux/Ubuntu常用快捷键

    问题描述:         Linux/Ubuntu常用快捷键   问题解决: +++++++++++++++++++ 全局系统 +++++++++++++++++++++ Alt + F1:相当于w ...

  9. failed creating the Direct3d device--debug

    D3DDEVTYPE_REF 使用REF设备,用软件模拟Direct3D API 照理说是为了让电脑能跑本机不能硬件执行的渲染命令的 但我 pDeviceSettings->d3d9.Devic ...

  10. iOS开发之ARC&MRC混用

    Xcode 项目中我们可以使用 ARC 和非 ARC 的混合模式. 如果你的项目使用的非 ARC 模式,则为 ARC 模式的代码文件加入 -fobjc-arc 标签. 如果你的项目使用的是 ARC 模 ...