剑指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}的一个旋转 ...
随机推荐
- Qt之qSetMessagePattern
简述 改变默认的消息处理输出. 允许改变qDebug().qWarning().qCritical().qFatal()的输出. 简述 占位符 示例 qSetMessagePattern QT_MES ...
- 利用range() 控制循环
s = ['a','b','c','d','e'] for i in range(len(s)):... if i < len(s)-1:... print s[i] a ...
- 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- GridView用法大全(转)
http://www.cnblogs.com/sufei/archive/2010/03/27/1698590.html
- MySQL备份的shell脚本
经过测试该脚本可以远程备份,但需要配置远程登录用户的权限,经过测试啊,在把这个脚本添加到计划任务的时候是无法识别mysql命令的(即使是将mysql添加到环境变量也无法识别,是因为/etc/cront ...
- poj3660 最短路/拓扑序
题意:有n头牛,为了给牛排顺序,给出了牛之间的胜负关系,具有传递性,问给出的胜负关系是否可以给这些牛排出唯一的顺序. 其实是个拓扑排序问题,牛的胜负关系就是有向边,然后判断是否有唯一的拓扑序就行.当然 ...
- 移动端动画使用transform提升性能
在移动端做动画,对性能要求较高而通常的改变margin属性是性能极低的,即使使用绝对定位改变top,left这些属性性能也很差因此应该使用transform来进行动画效果,如transform:tra ...
- 【LOI2005】【P1306】河流
树归题,本来比较简单,但是因为几个思想搞错了,所以卡了两天 原题: 几乎整个Byteland 王国都被森林和河流所覆盖.小点的河汇聚到一起,形成了稍大点的河.就这样,所有的河水都汇聚并流进了一条大河, ...
- linux设备驱动程序该添加哪些头文件以及驱动常用头文件介绍(转)
原文链接:http://blog.chinaunix.net/uid-22609852-id-3506475.html 驱动常用头文件介绍 #include <linux/***.h> 是 ...
- breakpoints
https://blogs.msdn.microsoft.com/visualstudioalm/2013/10/07/breakpoints-in-visual-studio-2013/ Using ...