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. 实用项目管理前台框架:EasyUI,ExtJs

    EasyUI项目管理框架,如图: 项目名称:微信管理平台 项目地址:http://www.cnblogs.com/hanyinglong/p/3236966.html#3007557 托管地址:htt ...

  2. 微信wap开发,页面显示元素不全-微信开发(asp.net)

    最近在开发的微信的微商城,出现这样一种情况: pc上浏览正常,但是一到手机上浏览就会缺少部分元素 解决办法: 找了很多原因,还通过uc浏览器把网页到存下来了,发现并没有缺少元素,只是没有显示出来,后来 ...

  3. 如何在cocos2dx lua的回调函数里面用self

    回调里的self是另一个不同的东西了,通常是触发回调的对象,或_G或nil ,视情况而定 我的 print(self) 输出 userdata function MyClass:sayFromCall ...

  4. CSS去除Chrome浏览器的控件默认样式

    html的input输入框在Chrome浏览器里是有默认样式的,当它获得焦点时,即使你没有为它设置:focus时的样式,Chrome浏览器还是会给它加上蓝色的边框,今天百度找到有个方法可以去除该默认样 ...

  5. 1231: [Usaco2008 Nov]mixup2 混乱的奶牛 - BZOJ

    Description 混乱的奶牛 [Don Piele, 2007] Farmer John的N(4 <= N <= 16)头奶牛中的每一头都有一个唯一的编号S_i (1 <= S ...

  6. hdoj 1102 Constructing Roads

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 分析:看到这题给出的都是矩阵形式,就知道了可以用Prim算法求MST. #include <i ...

  7. 深入浅出Java并发包—指令重排序

    前面大致提到了JDK中的一些个原子类,也提到原子类是并发的基础,更提到所谓的线程安全,其实这些类或者并发包中的这么一些类,都是为了保证系统在运行时是线程安全的,那到底怎么样才算是线程安全呢? Java ...

  8. 套题T6

    过节(festival.cpp/c/pas) Dxy帮老师们过教师节的时候需要购买礼物.货架上有n 种礼物,每种礼物有不同的个数.每种礼物有价值和花费两种属性,帮他算出最大可以得到的价值.M是带的钱数 ...

  9. 检测系统是X86系统,还是X64系统

    function IsWin64: Boolean; var Kernel32Handle: THandle; IsWow64Process: function(Handle: Windows.THa ...

  10. js错误:对象不支持此属性或方法

    对象不支持此属性或方法 错误原因: 可能是js的文件名和另外一个文件重复. 也有可能是js里的function和另外一个function名字重复. 也有可能是js里的function和页面的某一元素重 ...