• 有1千万个随机数,随机数的范围在1到1亿之间,将1到1亿之间没有在随机数中的数求出来


    package _interview_question
    
    import java.util.*
    import kotlin.collections.ArrayList
    
    /**
     * 有1千万个随机数,随机数的范围在1到1亿之间。现在要求写出一种算法,将1到1亿之间没有在随机数中的数求出来?
     * */
    class Solution9 {
        fun getNumberInOneBillion(){
    //        val TenMillion = 10000000
    //        val OneBillion = 100000000
            //for test
            val TenMillion = 10
            val OneBillion = 20
            val random = Random()
            val list = ArrayList<Int>()
            for (i in 1 .. TenMillion){//<-here is ten million
                list.add(random.nextInt(OneBillion))//<-here is one billion
            }
            val bitSet = BitSet(OneBillion)
            for (i in 1 .. TenMillion){
                //sets the bit at the specified index to true
                bitSet.set(list.get(i-1))
            }
            //println("range 0-one billion, the count of not including in random is:${bitSet.cardinality()})")
            //没有在随机数中的数字
            for (i in 1 .. OneBillion){
                if (!bitSet.get(i)){
                    println(i)
                }
            }
            println("===")
            //有在随机数中的数字
            for (i in 1 .. OneBillion){
                if (bitSet.get(i)){
                    println(i)
                }
            }
        }
    }
  • 相关阅读:
    Gitlab使用腾讯企业邮箱
    查看Binlog内容
    微信小游戏手记
    clickhouse手记
    腾讯云手记
    go框架gin
    go idea debug
    go手记
    crontab手记
    Laravel-cors 跨域
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13166550.html
Copyright © 2020-2023  润新知