用java找出2个数组中重复的数字_【Java】 剑指offer(1) 找出数组中重复的数字

时间:2025-03-27 21:00:58

本文参考自《剑指offer》一书,代码采用Java语言。

题目

在一个长度为n的数组里的所有数字都在0到n-1的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。例如,如果输入长度为7的数组{2, 3, 1, 0, 2, 5, 3},那么对应的输出是重复的数字2或者3。

思路

哈希表的思路拓展,重排数组:把扫描的每个数字(如数字m)放到其对应下标(m下标)的位置上,若同一位置有重复,则说明该数字重复。

(在动手写代码前应该先想好测试用例)

测试用例

1.数组中带一个或多个重复数字

2.数组中不包含重复的数字

3.无效输入测试用例(空数组,数组数字越界等)

完整Java代码

(含测试代码)

/**

* @Description 找出数组中重复的数字

*

* @author yongh

* @date 2018年7月16日 上午10:24:05

*/

/*

* 题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,

* 也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。例如,如果输入长度为7的数组{2, 3, 1, 0, 2, 5, 3},

* 那么对应的输出是重复的数字2或者3。

*/

public class FindDuplication {

/**

* 找到数组中一个重复的数字

* 返回-1代表无重复数字或者输入无效

*/

public int getDuplicate(int[] arr) {

if (arr == null || <= 0) {

("数组输入无效!");

return -1;

}

for (int a : arr) {

if (a < 0 || a > - 1) {

("数字大小超出范围!");

return -1;

}

}

for (int i = 0; i < ; i++) {

int temp;

while (arr[i] != i) {

if (arr[arr[i]] == arr[i])

return arr[i];

// 交换arr[arr[i]]和arr[i]

temp = arr[i];

arr[i] = arr[temp];

arr[temp] = temp;

}

}

("数组中无重复数字!");

return -1;

}

// ==================================测试代码==================================

/**

*数组为null

*/

public void test1() {

("test1:");

int[] a = null;

int dup = getDuplicate(a);

if (dup >= 0)

("重复数字为:" + dup);

}

/**

*数组无重复数字

*/

public void test2() {

("test2:");

int[] a = { 0, 1, 2, 3 };

int dup = getDuplicate(a);

if (dup >= 0)

("重复数字为:" + dup);

}

/**

*数组数字越界

*/

public void test3() {

("test3:");

int[] a = { 1, 2, 3, 4 };

int dup = getDuplicate(a);

if (dup >= 0)

("重复数字为:" + dup);

}

/**

*数组带重复数字

*/

public void test4() {

("test4:");

int[] a = { 1, 2, 3, 2, 4 };

int dup = getDuplicate(a);

if (dup >= 0)

("重复数字为:" + dup);

}

public static void main(String[] args) {

FindDuplication f = new FindDuplication();

f.test1();

f.test2();

f.test3();

f.test4();

}

}

test1:数组输入无效!

test2:数组中无重复数字!

test3:数字大小超出范围!

test4:重复数字为:

FindDuplication

复杂度

时间复杂度:O(n)

空间复杂度:O(1)

====================================================================

在牛客网中提交的代码如下(不含测试代码):

public class Solution {

// Parameters:

// numbers: an array of integers

// length: the length of array numbers

// duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;

// Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++

// 这里要特别注意~返回任意重复的一个,赋值duplication[0]

// Return value: true if the input is valid, and there are some duplications in the array number

// otherwise false

public boolean duplicate(int numbers[],int length,int [] duplication) {

if(numbers==null||length<=0)

return false;

for(int a:numbers){

if(a<0||a>=length)

return false;

}

int temp;

for(int i=0;i

while(numbers[i]!=i){

if(numbers[numbers[i]]==numbers[i]){

duplication[0]=numbers[i];

return true;

}

temp=numbers[i];

numbers[i]=numbers[temp];

numbers[temp]=temp;

}

}

return false;

}

}

这里有一个收获:在Java的方法中,可以用duplication[0]代替C/C++中的指针*duplication,从而在返回boolean的同时,也可以获得需要的数字。

剑指Offer:找出数组中出现次数超过一半的元素

题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include int ha ...

【剑指offer】找出数组中任意重复的数字(不修改数组),C++实现

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

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

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

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

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

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

【剑指offer】找出数组中任意一个重复的数字,C++实现

【剑指offer】找出数组中出现一次的两个数

2013-09-08 10:50:46 一个整型数组中,除了两个数字之外,其他数字都出现了2次,找出这两个只出现一次的数字,要求时间复杂度是O(N),空间复杂度是O(1). 小结: 任何数与0异或,结 ...

剑指offer28:找出数组中超过一半的数字。

1 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出 ...

《剑指offer》第三_一题(找出数组中重复的数字,可改变数组)

// 面试题3(一):找出数组中重复的数字 // 题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了, // 也不知道每个数字重复了几次.请 ...

随机推荐

javascript设计模式学习之五——策略模式

一.策略模式定义: 定义一些列的算法/规则,将它们封装起来,使得它们可以互相替换/组合使用.其目的在于将算法/规则封装起来,将算法/规则的使用与实现分离出来. 通过策略模式,可以减少算法计算过程中大量 ...

openerp学习笔记 计算字段支持搜索

示例1: # -*- encoding: utf-8 -*-import poolerimport loggingimport netsvcimport toolslogger = ...

leetcode@ [318] Maximum Product of Word Lengths (Bit Manipulations)

/problems/maximum-product-of-word-lengths/ Given a string array words, find the ...

Http(2)

1.传递的请求参数如何获取 GET方式: 参数放在URI后面 POST方式: 参数放在实体内容中 核心的API: request.getParameter("参数名");  根据参 ...

hdu1024

#include #include const int MAX = 1000005; using namespace std; int nu ...

angular ng-bind

Swing-JFileChooser的使用

JFileChooser文件选择器是Swing中经常用到的一个控件.它的使用主要包含以下几个参数: 1.当前路径.也就是它第一次打开时所在的路径,许多软件喜欢设置为桌面. 2.文件过滤器.通过设置文件 ...

vscode调试golang环境搭建及配置

准备VSCode 在官网下载最新版的VSCode: 安装Golang插件 打开扩展面板 VSCode->查看->扩展 找到Go插件 在搜索框里输入Go, 找到第二行写有 Rich Go l ...

利用sqlserver sa更改系统密码

--允许修改高级属性 sp_configure go reconfigure go --启用扩展存储命令 sp_configure go reconfigure go --系统添加一个windows用 ...

简单说说SpringMVC

距离上一次开发SpringMVC项目已经过去了大半年,有些细节已经开始遗忘,今天复习一下 先从标签说起: 和struts有各种配置文件不同,spring用标签开发. 1.@Controller在Spr ...