剑指offer系列24---数组中重复的数字
* 【24】
* 【题目】在一个长度为n的数组里的所有数字都在0到n-1的范围内。
* 数组中某些数字是重复的,但不知道有几个数字是重复的。
* 也不知道每个数字重复几次。
* 请找出数组中任意一个重复的数字。
* 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3。
书上方法:
package com.exe6.offer;
/**
* 【24】
* 【题目】在一个长度为n的数组里的所有数字都在0到n-1的范围内。
* 数组中某些数字是重复的,但不知道有几个数字是重复的。
* 也不知道每个数字重复几次。
* 请找出数组中任意一个重复的数字。
* 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3。
* @author WGS
*
*/
public class DuplicationNums { @SuppressWarnings("unused")
public boolean DuplicationNums(int[] nums){
if(nums==null ||nums.length <=0) return false;
int len=nums.length;
for(int i=0;i<len;i++){
if(nums[i]>=len){
System.out.println("输入的数不符合要求!");
return false;
}
} for(int i=0;i<len;i++){
while(nums[i]!=i){
if(nums[i]==nums[nums[i]]){ return true;
}else{
int temp=nums[i];//
nums[i]=nums[nums[i]];//
nums[temp]=temp;
}
}
}
return false;
}
public static void main(String[] args) {
int numbers[]=new int[]{2,3,1,0,2,5,3};
DuplicationNums d=new DuplicationNums();
boolean b=d.DuplicationNums(numbers);
System.out.println(b);
} }
博主代码,可以显示指定数字重复的次数:
package com.exe6.offer; import java.util.HashMap;
import java.util.Map; /**
* 【24】
* 【题目】在一个长度为n的数组里的所有数字都在0到n-1的范围内。
* 数组中某些数字是重复的,但不知道有几个数字是重复的。
* 也不知道每个数字重复几次。
* 请找出数组中任意一个重复的数字。
* 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3。
* @author WGS
*
*/
public class DuplicationNums2 { public boolean DuplicationNums2(int[] nums,int [] duplication){
if(nums==null ||nums.length <=1) return false;
int len=nums.length; for(int i=0;i<len;i++){
if(nums[i]>=len){
System.out.println("输入的数不符合要求!");
return false;
}
}
Map<Integer,Integer> counter=new HashMap<>();
for(int i=0;i<len;i++){
while(nums[i]!=i){
if(counter.containsKey(nums[i])){
duplication[0]=nums[i];
return true;
}else{
counter.put(nums[i], new Integer(1));
}
}
}
return false;
} public static void main(String[] args) {
int numbers[]=new int[]{2,3,1,0,2,5,3};
DuplicationNums2 d=new DuplicationNums2();
boolean b=d.DuplicationNums2(numbers,new int[1]);
System.out.println(b);
} }
还有更好的方法:
import java.util.Set;
import java.util.HashSet;
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<2 ){
return false;
}
Set<Integer> set=new HashSet<>();
for(int i=0;i<length;i++){
if(!set.add(numbers[i])){
duplication[0]=numbers[i];
return true;
}else{
set.add(numbers[i]);
}
}
return false;
}
}
剑指offer系列24---数组中重复的数字的更多相关文章
- 【剑指 Offer】03.数组中重复的数字
题目描述 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中 ...
- 剑指offer.找出数组中重复的数字
题目: 给定一个长度为 n 的整数数组 nums,数组中所有的数字都在 0∼n−1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中任意一个重复的数 ...
- 《剑指offer》-找到数组中重复的数字
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- 《剑指offer》旋转数组中的最小数字
本题来自<剑指offer> 旋转数组中的最小数字 题目: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例 ...
- 剑指offer35题:第一个只出现一次的字符+剑指offer55题:字符流中第一个不重复的字符+剑指offer51题:数组中重复的数字
在看剑指offer的时候,感觉这三个题目很像,都是用哈希表可以解决,所以把这三个题整理出来,以供复习. 剑指offer35题:第一个只出现一次的字符 题目描述:在字符串中找出第一个只出现一次的字符.如 ...
- 【剑指Offer】旋转数组中的最小数字 解题报告(Python)
[剑指Offer]旋转数组中的最小数字 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://www.nowcoder.com/ta/coding-intervie ...
- 【剑指offer】03.数组中重复的数组
剑指 Offer 03. 数组中重复的数字 知识点:数组:哈希表:萝卜占坑思想 题目描述 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些 ...
- 【剑指offer】50.数组中重复出现的数字
50.数组中重复出现的数字 知识点:数组:Set的不可重复性 题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重 ...
- 剑指offer——11旋转数组中最小的数字
题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转 ...
随机推荐
- Think Python - Chapter 12 Tuples
12.1 Tuples are immutable(元组是不可变的)A tuple is a sequence of values. The values can be any type, and t ...
- 【转】7 Tips to Speed Up Eclipse
技巧一:运行最新版本的JDK和Eclipse 通常,新版本的JDK和Eclipse都会有性能上的优化.请确保你使用的是64位Eclipse并且使用了Oracle的JDK.对于网络开发,需要使用Ecli ...
- POJ 1195 Mobile phones(二维树状数组)
Mobile phones Time Limit: 5000MS Mem ...
- Linux嵌入式入门
虚拟机Linux系统网络配置: 1.Vmware网络设置 虚拟机设置->网路适配器->网络连接 桥接模式:能提供独立的IP地址的情况下使用 NAT模式:一台计算机只能使用一个I ...
- 磁盘与目录的容量[转自vbird]
磁盘与目录的容量 现在我们知道磁盘的整体数据是在 superblock 区块中,但是每个各别文件的容量则在 inode 当中记载的. 那在文字接口底下该如何叫出这几个数据呢?底下就让我们来谈一谈这两个 ...
- (转)A Beginner's Guide To Understanding Convolutional Neural Networks Part 2
Adit Deshpande CS Undergrad at UCLA ('19) Blog About A Beginner's Guide To Understanding Convolution ...
- 论文笔记之:Playing for Data: Ground Truth from Computer Games
Playing for Data: Ground Truth from Computer Games ECCV 2016 Project Page:http://download.visinf.tu- ...
- Linux 挂载新硬盘
Linux 的硬盘识别 在 /dev/ 下建立相应的设备文件.如 sda 表示第一块 SCSI 硬盘 hda 表示第一块 IDE 硬盘(即连接在第一个 IDE 接口的 Master 口上) scd0 ...
- HTTPS(SSL)详解以及PHP调用方法
HTTPS 详解 1. 两个加密秘钥的概念 (1) 对称加密 即加密的秘钥和解密的秘钥一样 (2) 非对称加密 即加密的秘钥和解密的秘钥不一样, 分别称为公钥 和 私钥, 公钥完全公开 私钥解密者 ...
- 如何用OCR图文识别软件在文档里复制内容
ABBYY FineReader 12是一款OCR图文识别软件,可从文档中复制文本.图片和表格,粘贴到其他应用程序中.无需识别整个文档(关于ABBYY FineReader识别文档的文章,请参考解析A ...