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. 网络笔记01-3 socket 实现百度页面的两种方式

    scoket 实现百度页面的两种方式: 1.利用系统自带    //1.创建URL NSURL *url=[NSURL URLWithString:@"http://m.baidu.com& ...

  2. 怎么查看其它apk里面的布局代码及资源

    今天才看到的好方法, 将你要的apk文件的后缀名改为zip,解压就可以了. --------------------------------- 提示:有时候系统会自动隐藏你的后缀名的,这时候就需要你将 ...

  3. sqlserver 行转列、列转行[转]

    转自:http://www.cnblogs.com/luofuxian/archive/2012/02/23/2364328.html Sql Server 行转列.列转行   创建表:   CREA ...

  4. Codeforces Round #360 (Div. 2) E. The Values You Can Make 01背包

    题目链接: 题目 E. The Values You Can Make time limit per test:2 seconds memory limit per test:256 megabyte ...

  5. 【POJ】【2960】S-Nim

    博弈论 这题跟 BZOJ 1874 取石子游戏 差不多 先暴力求出10000以内的SG函数(利用定义来求即可) 然后每次询问直接将SG值异或起来即可…… Source Code Problem: Us ...

  6. isEmpty()与equals()、==“”区别

    isEmpty方法源码:public static boolean isEmpty(String str) { return (str == null) || (str.length() == 0); ...

  7. FolderBrowserDialog 成员

    http://msdn.microsoft.com/zh-cn/library/system.windows.forms.folderbrowserdialog_members(v=vs.80).as ...

  8. 【Asp.Net MVC-视频】

    Asp.Net MVC官网网发布的pluralsight视频教学: http://pluralsight.com/training/Player?author=scott-allen&name ...

  9. Windows 进程通信 之 DDE技术

    DDE (Dynamic Data Exchange,DDE)动态数据交换,是一种进程间通信机制,它最早是随着Windows由微软提出的.当前大部分软件仍旧支持DDE,但最近十年里微软已经停止发展DD ...

  10. ZOJ 3778 Talented Chef(找规律,模拟计算,11届ACM省赛,简单)

    题目链接 2014年浙江省赛C题,当时觉得难,现在想想这题真水.. 找规律: 若   最大的那个步骤数*m-总和>=0,那么答案就是 最大的那个步骤数 . 否则  就要另加上不够的数量,具体看代 ...