* 【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---数组中重复的数字的更多相关文章

  1. 【剑指 Offer】03.数组中重复的数字

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

  2. 剑指offer.找出数组中重复的数字

    题目: 给定一个长度为 n 的整数数组 nums,数组中所有的数字都在 0∼n−1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中任意一个重复的数 ...

  3. 《剑指offer》-找到数组中重复的数字

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

  4. 《剑指offer》旋转数组中的最小数字

    本题来自<剑指offer> 旋转数组中的最小数字 题目: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例 ...

  5. 剑指offer35题:第一个只出现一次的字符+剑指offer55题:字符流中第一个不重复的字符+剑指offer51题:数组中重复的数字

    在看剑指offer的时候,感觉这三个题目很像,都是用哈希表可以解决,所以把这三个题整理出来,以供复习. 剑指offer35题:第一个只出现一次的字符 题目描述:在字符串中找出第一个只出现一次的字符.如 ...

  6. 【剑指Offer】旋转数组中的最小数字 解题报告(Python)

    [剑指Offer]旋转数组中的最小数字 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://www.nowcoder.com/ta/coding-intervie ...

  7. 【剑指offer】03.数组中重复的数组

    剑指 Offer 03. 数组中重复的数字 知识点:数组:哈希表:萝卜占坑思想 题目描述 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些 ...

  8. 【剑指offer】50.数组中重复出现的数字

    50.数组中重复出现的数字 知识点:数组:Set的不可重复性 题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重 ...

  9. 剑指offer——11旋转数组中最小的数字

    题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转 ...

随机推荐

  1. Word Ladder II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  2. jquery zclip 复制黏贴功能不能实现

    按照http://www.steamdev.com/zclip/我实现一个简单的zclip test 以下是我的测试code: <!DOCTYPE html> <html> & ...

  3. 刻通云KeyTone Cloud测试

    注:本文转自陈沙克的博客,原文见http://www.chenshake.com/carved-through-the-keytone-cloud-cloud-testing/ 一直都很希望有更多的O ...

  4. mysql样例数据库employees

    Oracle和sqlserver都有基于员工信息的样例数据库,同样mysql上也是有的. 给出一个连接地址https://github.com/datacharmer/test_db. 下载后直接调用 ...

  5. 2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp

    QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  6. POJ 3233 Matrix Power Series 矩阵快速幂

    设S[k] = A + A^2 +````+A^k. 设矩阵T = A[1] 0 E E 这里的E为n*n单位方阵,0为n*n方阵 令A[k] = A ^ k 矩阵B[k] = A[k+1] S[k] ...

  7. HTML初讲

    整理老师所讲: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  8. as3 中 textfiled的htmltext 的常用属性

    http://blog.sina.com.cn/s/blog_6d193c030100x6ud.html <a> 超链接标签 属性:href: 链接地址 target: 目标窗口 可取值为 ...

  9. Unity坐标系

    Unity 使用的是左手坐标系

  10. rsyslog日志服务的配置文件分析

    基于rsyslog日志服务的日志 在不同的LINUX系统,实现的软件略有不同. syslog,rsyslog,syslog-ng,用于实现系统日志的管理. [root@asianux4 ~]# rpm ...