剑指offer第二版-3.数组中重复的数
面试题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.数组中重复的数的更多相关文章
- 【Java】 剑指offer(1) 找出数组中重复的数字
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字 ...
- 【剑指Offer】50、数组中重复的数字
题目描述: 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果 ...
- 剑指offer(50)数组中重复的数字
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- 剑指offer五十之数组中重复的数字
一.题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- 剑指Offer01之二维数组中查找目标数
剑指Offer之二维数组中查找目标数 题目描述 在一个二维数组中(每个一维数组的长度相等),每一行都是从左到右递增的顺序排序,每一列都是从上到下递增的顺序排序,输入这样一个二维数组和一个整数,判断 ...
- 《剑指offer(第二版)》面试题55——判断是否为平衡二叉树
一.题目大意 输入一颗二叉树,判断该二叉树是否为平衡二叉树(AVL树). 二.题解 <剑指offer>上给出了两种解决方式: 1.第一种是从根节点开始,从上往下遍历每个子节点并计算以子节点 ...
- 剑指Offer - 九度1348 - 数组中的逆序对
剑指Offer - 九度1348 - 数组中的逆序对2014-01-30 23:19 题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个 ...
- 剑指Offer - 九度1370 - 数组中出现次数超过一半的数字
剑指Offer - 九度1370 - 数组中出现次数超过一半的数字2013-11-23 03:55 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组 ...
- 剑指Offer - 九度1351 - 数组中只出现一次的数字
剑指Offer - 九度1351 - 数组中只出现一次的数字2013-11-23 01:23 题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. ...
随机推荐
- GIS基础软件及操作(八)
原文 GIS基础软件及操作(八) 练习八.地理建模 地理建模:Model Builder 土壤侵蚀危险性建模分析 认识ModelBuilder操作界面 1: 添加硬盘上的数据或工具到模型中,数据也可以 ...
- 创建dll动态链接库,并使用java调用
参考文章:http://www.cnblogs.com/matthew-2013/p/3480296.html http://blog.csdn.net/g710710/article/details ...
- 无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS" 之间的排序规则冲突。
无法解决 equal to 操作中 "SQL_Latin1_General_CP1_CI_AS" 和 "Chinese_PRC_CI_AS" 之间的排序规则冲突 ...
- .NET解析xml字符串,通过反射给实体类对象赋值,获取实体类数据列表
/// <summary> /// 解析xml字符串 转换为实体类列表数据 /// </summary> /// <param name="xmlStr&quo ...
- Qt技术优势
1. Qt这个C++的图形库由Trolltech在1994年左右开发.它可以运行在Windows,Mac OS X, Unix,还有像Sharp Zaurus这类嵌入式系统中.Qt是完全面向对象的. ...
- java.lang.Integer cannot be cast to java.lang.String
错误原因是类型转换! 说Integer 类型不能转成String类型. 解决办法: 将错误中的(String)强制转换类型修改为 object.toString() toString方法是Ja ...
- Python之二叉树Binarytree
二叉树是树的简化版,除根节点之外的所有节点都有一个父节点,任意节点都可以最多有一个左子节点和右子节点. 二叉树的遍历是非常重要的算法,主要分为深度优先遍历和广度优先遍历. 其中深度优先遍历按照访问根节 ...
- 【Java源码】集合类-JDK1.8 哈希表-红黑树-HashMap总结
JDK 1.8 HashMap是数组+链表+红黑树实现的,在阅读HashMap的源码之前先来回顾一下大学课本数据结构中的哈希表和红黑树. 什么是哈希表? 在存储结构中,关键值key通过一种关系f和唯一 ...
- Spark学习之路(二)—— Spark开发环境搭建
一.安装Spark 1.1 下载并解压 官方下载地址:http://spark.apache.org/downloads.html ,选择Spark版本和对应的Hadoop版本后再下载: 解压安装包: ...
- node实现文件拷贝2
https://www.cnblogs.com/coding4/p/7495968.html 文件拷贝NodeJS 提供了基本的文件操作 API,但是像文件拷贝这种高级功能就没有提供,因此我们先拿文件 ...