面试题3:数组中重复的数

题目要求:
在一个长度为n的数组中,所有数字的取值范围都在[0,n-1],但不知道有几个数字重复或重复几次,找出其中任意一个重复的数字。

解法比较:

/**
* Copyright(C) 2019 Hangzhou Differsoft Co., Ltd. All rights reserved.
*
*/
package com.java.offer; /**
* 一个长度为n的数组,值的范围在0~n-1内,有一个或多个数字重复,求其中任意一个
*
* @since 2019年1月29日 下午3:46:06
* @author xuchao
*
*/
public class P3_DuplicationInArray { // 方法一:暴力求解,不会修改原始数据,时间复杂度o(n^2),空间复杂度o(1)
public static int getDuplication1(int[] data) {
if (data == null || data.length < 2) {
return -1;
}
for (int i = 0; i < data.length - 1; i++) {
for (int j = i + 1; j < data.length; j++) {
if (data[j] == data[i]) {
return data[j];
}
}
}
return -1;
}
/**
* 方法二:排序(使用快排),会修改原始数据,时间复杂度o(nlogn),空间复杂度o(1)
* 1.使用内置排序(双轴快排),Arrays.sort(data)
* 2.自己手写快排
*/
public static int getDuplication2(int[] data) {
if (data == null || data.length < 2) {
return -1;
}
// 可以使用内置排序(双轴快排)
// Arrays.sort(data);
quickSort(data, 0, data.length - 1);
for (int i = 0; i < data.length - 1; i++) {
if (data[i + 1] == data[i]) {
return data[i + 1];
}
}
return -1;
} public static void quickSort(int[] data, int start, int end) {
if (start >= end) {
return;
}
int mid = partition(data, start, end);
quickSort(data, start, mid - 1);
quickSort(data, mid + 1, end);
} public static int partition(int[] data, int start, int end) {
int base = data[start];
int i=start,j=end;
while (i < j) {
while (i < j && data[j] >= base) {
j--;
}
while (i < j && data[i] <= base) {
i++;
}
if (i < j) {
int t = data[i];
data[i] = data[j];
data[j] = t;
}
}
data[start] = data[j];
data[j] = base;
return j;
} // 方法三:借助哈希表,不会修改原始数据,时间复杂度o(n),空间复杂度o(n)
public static int getDuplication3(int[] data) {
if (data == null || data.length < 2) {
return -1;
}
int[] a = new int[data.length];
for (int t : data) {
if (a[t] == 1) {
return t;
} else {
a[t] = 1;
}
}
return -1;
} // 方法四:根据数字特点排序,会修改原始数据,时间复杂度o(n),空间复杂度o(1)
public static int getDuplication4(int[] data) {
if (data == null || data.length < 2) {
return -1;
}
for (int i = 0; i < data.length; i++) {
while (data[i] != i) {
if (data[i] == data[data[i]]) {
return data[i];
} else {
int temp = data[i];
data[i] = data[temp];
data[temp] = temp;
}
}
}
return -1;
} public static void main(String[] args) {
int[] data1 = { 2, 5, 6, 4, 6, 3, 1 };
// 不修改原始数据
System.out.println(getDuplication1(data1));
System.out.println(getDuplication3(data1)); int[] data2 = { 2, 5, 6, 4, 6, 3, 1 };
System.out.println(getDuplication2(data2)); int[] data3 = { 4, 3, 5, 1, 5, 0 };
System.out.println(getDuplication4(data3));
}
}

剑指offer第二版-3.数组中重复的数的更多相关文章

  1. 【Java】 剑指offer(1) 找出数组中重复的数字

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字 ...

  2. 【剑指Offer】50、数组中重复的数字

      题目描述:   在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果 ...

  3. 剑指offer(50)数组中重复的数字

    题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...

  4. 剑指offer五十之数组中重复的数字

    一.题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...

  5. 剑指Offer01之二维数组中查找目标数

    剑指Offer之二维数组中查找目标数 题目描述 ​ 在一个二维数组中(每个一维数组的长度相等),每一行都是从左到右递增的顺序排序,每一列都是从上到下递增的顺序排序,输入这样一个二维数组和一个整数,判断 ...

  6. 《剑指offer(第二版)》面试题55——判断是否为平衡二叉树

    一.题目大意 输入一颗二叉树,判断该二叉树是否为平衡二叉树(AVL树). 二.题解 <剑指offer>上给出了两种解决方式: 1.第一种是从根节点开始,从上往下遍历每个子节点并计算以子节点 ...

  7. 剑指Offer - 九度1348 - 数组中的逆序对

    剑指Offer - 九度1348 - 数组中的逆序对2014-01-30 23:19 题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个 ...

  8. 剑指Offer - 九度1370 - 数组中出现次数超过一半的数字

    剑指Offer - 九度1370 - 数组中出现次数超过一半的数字2013-11-23 03:55 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组 ...

  9. 剑指Offer - 九度1351 - 数组中只出现一次的数字

    剑指Offer - 九度1351 - 数组中只出现一次的数字2013-11-23 01:23 题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. ...

随机推荐

  1. Word Embeddings: Encoding Lexical Semantics

    Word Embeddings: Encoding Lexical Semantics Getting Dense Word Embeddings Word Embeddings in Pytorch ...

  2. Delphi皮肤之 - 图片按钮

    效果如图,支持普通.移上去.按下.弹起.禁用5种状态. unit BmpBtn; interface uses Windows, Messages, SysUtils, Classes, Graphi ...

  3. windows service 之访问权限(有NetworkService和LocalSystem的区分)

    最近写了一个关于从局域网的算机上下载文件的winodws service,最初认为应该没什么大的问题.通过本地的调试也没发现问题.但是当我把程序发布后发现服务报错“访问路径被拒绝”,我的第一感觉,肯定 ...

  4. [java代码库]-简易计算器(第二种)

    [java代码库]-简易计算器(第二种) 第二种方案:在程序中不使用if/switch……case等语句,完成计算器功能. <html> <head> <title> ...

  5. hdu4633_Polya定理

    典型的Polya定理,还算比较简单,比赛的时候知道是Polya定理但是根本没留出时间去搞,有点小遗憾. 思路:根据Burnside引理,等价类个数等于所有的置换群中的不动点的个数的平均值,根据Poly ...

  6. linux oracle 启动全过程

    一:启动oracle [root@ccoracle ~]# su -l oracle [oracle@ccoracle ~]$ sqlplus /nolog SQL*Plus: Release 10. ...

  7. mac下 编译php的 openssl

    编译openssl.so tar zxvf php-7.2.8.tar.gz# 进入PHP的openssl扩展模块目录cd php-7.2.8/ext/openssl/brew install ope ...

  8. django自带的cache

    cache语法 from django.core.cache import cache #存入内存 cache.set("aaa",123) #从内存中获取 cache.get(& ...

  9. lodop+art-template实现web端漂亮的小票样式打印

    一. 现状 由于之前采用Lodop打印控件(商业版付费,可以使用免费版 但是会有水印)去打印小票,是一行一行的打印,但是不满足UI给到复杂布局的小票样式,所以得重新考虑如何来实现. 二. 介绍 art ...

  10. 第二章 在Linux上部署.net core

    项目目标部署环境:CentOS 7+ 项目技术点:.netcore2.0 + Autofac +webAPI + NHibernate5.1 + mysql5.6 + nginx 开源地址:https ...