zoukankan      html  css  js  c++  java
  • 数组中重复的数字

    题目描述

    在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
    import java.util.HashSet;
    import java.util.Set;
    
    /**
     * 
     * @author gentleKay
     * 题目描述
     * 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 
     * 数组中某些数字是重复的,但不知道有几个数字是重复的。
     * 也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 
     * 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。
     */
    
    public class Main49 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		int[] numbers = {1,2,3,4,2,5,3};
    		int[] duplication = new int[2];
    		System.out.println(Main49.duplicate(numbers, numbers.length, duplication));
    	}
    
    	public static boolean duplicate(int numbers[],int length,int [] duplication) {
    		if (length == 0) {
    			return false;
    		}
    		Set<Integer> set = new HashSet<>();
    		for (int i=0;i<length;i++) {
    			if (!set.contains(numbers[i])) {
    				set.add(numbers[i]);
    			}else {
    				duplication[0] = numbers[i];
    //				System.out.println(duplication[0]);
    				return true;
    			}
    		}
    		return false;
        }
    	
    
    }
    

      

  • 相关阅读:
    python-包和模块
    电荷量 电流 电压 功率
    h264 aac 封装 flv
    flv 解封装
    flv格式详解+实例剖析
    rtmp直播推流(一)--flv格式解析与封装
    nginx statistics in multi-workers
    windows平台最简单的rtmp/hls流媒体服务器
    Node-Media-Server
    ffmpeg 基本数据结构和对象: AVPacket、AVPicture、AVFrame
  • 原文地址:https://www.cnblogs.com/strive-19970713/p/11196395.html
Copyright © 2011-2022 走看看