【Java】 剑指offer(1) 找出数组中重复的数字
本文参考自《剑指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 || arr.length <= 0) {
System.out.println("数组输入无效!");
return -1;
}
for (int a : arr) {
if (a < 0 || a > arr.length - 1) {
System.out.println("数字大小超出范围!");
return -1;
}
}
for (int i = 0; i < arr.length; 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;
}
}
System.out.println("数组中无重复数字!");
return -1;
} // ==================================测试代码==================================
/**
*数组为null
*/
public void test1() {
System.out.print("test1:");
int[] a = null;
int dup = getDuplicate(a);
if (dup >= 0)
System.out.println("重复数字为:" + dup);
} /**
*数组无重复数字
*/
public void test2() {
System.out.print("test2:");
int[] a = { 0, 1, 2, 3 };
int dup = getDuplicate(a);
if (dup >= 0)
System.out.println("重复数字为:" + dup);
} /**
*数组数字越界
*/
public void test3() {
System.out.print("test3:");
int[] a = { 1, 2, 3, 4 };
int dup = getDuplicate(a);
if (dup >= 0)
System.out.println("重复数字为:" + dup);
} /**
*数组带重复数字
*/
public void test4() {
System.out.print("test4:");
int[] a = { 1, 2, 3, 2, 4 };
int dup = getDuplicate(a);
if (dup >= 0)
System.out.println("重复数字为:" + 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<length;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的同时,也可以获得需要的数字。
【Java】 剑指offer(1) 找出数组中重复的数字的更多相关文章
- 剑指Offer:找出数组中出现次数超过一半的元素
题目:找出数组中出现次数超过一半的元素 解法:每次删除数组中两个不同的元素,删除后,要查找的那个元素的个数仍然超过删除后的元素总数的一半 #include <stdio.h> int ha ...
- 【剑指offer】找出数组中任意重复的数字(不修改数组),C++实现
原创博文,转载请注明出处! # 题目 在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的.请找出数组中任意一个重复的数字,但不能修改输入的数组.例如,如果输入长度 ...
- 【剑指Offer】50、数组中重复的数字
题目描述: 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果 ...
- 剑指offer(50)数组中重复的数字
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- 剑指offer五十之数组中重复的数字
一.题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- 【剑指offer】找出数组中任意一个重复的数字,C++实现
原创博文,转载请注明出处! # 题目 # 思路 对于长度为n的数组,范围为0~n-1的数字而言,如果不粗在重复数字,则排序后数组元素和数组角标相同.如果存在重复数字,则在排序的过程中会出现不同下标对应 ...
- 【剑指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》-- 题目一:找出数组中重复的数字(Python多种方法实现)
数组中重复的数字 最近在复习算法和数据结构(基于Python实现),然后看了Python的各种"序列"--比如列表List.元组Tuple和字符串String,后期会写一篇博客介绍 ...
随机推荐
- Linux - 日志处理一
Linux 日志处理 history # 历时命令默认1000条 HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S " # 让history命令显示具体时间 hi ...
- .net 重新注册
今天同事问 一个IIS 的监控站点 .net 出现问题:对于windows 一般都停留在重启生效思想:然并没有生效: 于是建议重新注册.NET : 一般出现原因: 在默认安装路径 重启注册: 默认的安 ...
- 第16月第6天 vs2005 lseek directdraw
1. //_lseek(file_handle, -(int)pbitmap->bitmapinfoheader.biSizeImage, SEEK_END); SetFilePointer(( ...
- adb的使用
前面配置了环境变量,可以在计算机任何位置打开cmd窗口使用adb. 连接android应用 使用connect命令连接盒子的ip(要确保电脑所连接的网络和盒子是一个网络) 抓日志 抓取某一个操作过程的 ...
- js_原生js实现上拉加载更多的功能。
1.人生啊,是我莽撞了啊.这是我公司一个喜欢读书的女孩子的个性签名,喜欢哪些句子,不悲伤,却切切实实的令人喜好. 2.写程序是一件很直接明了的事情,明白了就是明白了,不懂就是不懂,不懂装懂的会让你走很 ...
- Android Studio导入系统 jar包,编译时优先于查找系统SDK
https://www.cnblogs.com/bluestorm/p/6744140.html
- 20165230田坤烨《网络对抗》Exp1 PC平台逆向破解
实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShe ...
- 【ANT】ant使用
官网:https://ant.apache.org/,task介绍:https://ant.apache.org/manual/index.html 0.介绍: Ant的构建文件当开始一个新的项目时, ...
- 重新学习Servlet二
重新学习Servlet public abstract class HttpServlet extends GenericServlet package com.xh.test.api; import ...
- Codeplex最流行25个开源项目
1. VMukti富媒体协作平台 2. Microsoft SQL Server Product Samples: Engine 3. Patterns & Practices: Enterp ...