当前位置: 首页 > news >正文

其它区便宜营销型网站建设微信app小程序开发

其它区便宜营销型网站建设,微信app小程序开发,软件资源网站推荐,用wordpress做官网题目: 给定一个非负整数 n,请计算 0 到 n 之间的每个数字的二进制表示中 1 的个数,并输出一个数组 示例: 1、 输入: n 2 输出: [0,1,1] 解释: 0 --> 0 1 --> 1 2 --> 10 2、 输入: n 5 输出: [0,1,1,2,1,2] 解释: 0 …

题目:

给定一个非负整数 n,请计算 0 到 n 之间的每个数字的二进制表示中 1 的个数,并输出一个数组

示例:

1、

输入: n = 2
输出: [0,1,1]
解释: 
0 --> 0
1 --> 1
2 --> 10

2、

输入: n = 5
输出: [0,1,1,2,1,2]
解释:
0 --> 0
1 --> 1
2 --> 10
3 --> 11
4 --> 100
5 --> 101

解题思路:

  1. 先把从 0 到 n 的非负整数,放到数组里
  2. 把这些非负整数都转换为二进制
  3. 判断他们当中 1 的个数
  4. 把二进制中的 0 和 1 相加,然后输出成数组
  5. 数组中数的和就是这些数当中 1 的个数

 

 

 

 

部分编程语言有相应的内置函数用于计算给定的整数的二进制表示中的 111 的数目,例如 Java\texttt{Java}Java 的 Integer.bitCount\texttt{Integer.bitCount}Integer.bitCount,C++\texttt{C++}C++ 的 __builtin_popcount\texttt{\_\_builtin\_popcount}__builtin_popcount,Go\texttt{Go}Go 的 bits.OnesCount\texttt{bits.OnesCount}bits.OnesCount 等,

方法一:Brian Kcrnighan 算法

最直观的做法是对从 0 到 n 的每个整数直接计算【一比特数】。每个 int 型的数都可以用 32 位二进制数表示,只有遍历其二进制表示的每一位即可得到 1 的数目。

利用 Brian Kcrnighan 算法,可以在一定程度上进一步提升计算速度。

Brian Kcrnighan算法的原理:

对于任意整数 x,令 x = x&(x - 1),该运算将 x 的二进制表示的最后一个 1 变成 0。因此,对 x 重复该操作,直到 x 变成 0,则操作次数即为 x 的【一比特数】

 

func onesCount(x int) (ones int) {for ; x > 0; x &= x - 1 {ones++}return
}func countBits(n int) []int {bits := make([]int, n+1)for i := range bits {bits[i] = onesCount(i)}return bits
}

 

方法二:动态规划 —— 最高有效位

func countBits(n int) []int {bits := make([]int, n+1)highBit := 0for i := 1, i <= n; i++ {if i&(i-1) == 0 {highBit = i}bits[i] = bits[i-highBit] + 1}return bits
}

 

 

 

方法三:动态规划 —— 最低有效位

 

 

func countBits(n int) []int {bits := make([]int, n+1)for i := 1; i <= n; i++ {bit[i] = bits[i>>1] + i&1}return bits
}

 

方法四:动态规划 —— 最低设置位

func countBits(n int) []int {bits := make([]int, n+1)for i := 1; i <= n; i++ {bits[i] = bits[i&(i-1)] + 1}return bits
}

 


文章转载自:
http://dinncosubversive.ssfq.cn
http://dinncotransactinide.ssfq.cn
http://dinncomolokai.ssfq.cn
http://dinncominna.ssfq.cn
http://dinncolaevulose.ssfq.cn
http://dinncoretinocerebral.ssfq.cn
http://dinncochoreograph.ssfq.cn
http://dinncobeneficence.ssfq.cn
http://dinncotussive.ssfq.cn
http://dinncocalmbelt.ssfq.cn
http://dinncoamesace.ssfq.cn
http://dinncohirsutulous.ssfq.cn
http://dinncodisentrancement.ssfq.cn
http://dinncofranchiser.ssfq.cn
http://dinncoscottishry.ssfq.cn
http://dinncotransthoracic.ssfq.cn
http://dinncoozonolysis.ssfq.cn
http://dinncobeneficiary.ssfq.cn
http://dinncomid.ssfq.cn
http://dinncosnugly.ssfq.cn
http://dinncopajama.ssfq.cn
http://dinncoballetically.ssfq.cn
http://dinncobroth.ssfq.cn
http://dinncodilative.ssfq.cn
http://dinncosugarcane.ssfq.cn
http://dinncoautocritcal.ssfq.cn
http://dinncolightstruck.ssfq.cn
http://dinncoadvantageous.ssfq.cn
http://dinncocartelize.ssfq.cn
http://dinncoemmesh.ssfq.cn
http://dinncoclypeated.ssfq.cn
http://dinncostride.ssfq.cn
http://dinncowettest.ssfq.cn
http://dinncosmartly.ssfq.cn
http://dinncounassisted.ssfq.cn
http://dinncocrackling.ssfq.cn
http://dinncopurification.ssfq.cn
http://dinncoprofession.ssfq.cn
http://dinncocarene.ssfq.cn
http://dinncofluency.ssfq.cn
http://dinncomislay.ssfq.cn
http://dinncogroundhog.ssfq.cn
http://dinncokanpur.ssfq.cn
http://dinncomorphophonics.ssfq.cn
http://dinncolongshoreman.ssfq.cn
http://dinncoinsensate.ssfq.cn
http://dinncosulphurweed.ssfq.cn
http://dinncocornily.ssfq.cn
http://dinncoplough.ssfq.cn
http://dinncotumbling.ssfq.cn
http://dinncoinsufferable.ssfq.cn
http://dinncoshellless.ssfq.cn
http://dinncoordinant.ssfq.cn
http://dinncomealanguage.ssfq.cn
http://dinncoowner.ssfq.cn
http://dinncolichen.ssfq.cn
http://dinncobumpkin.ssfq.cn
http://dinncocounseling.ssfq.cn
http://dinncounreconstructible.ssfq.cn
http://dinncoisorhythm.ssfq.cn
http://dinncobergall.ssfq.cn
http://dinncoblest.ssfq.cn
http://dinncoaphaeresis.ssfq.cn
http://dinncotyrolite.ssfq.cn
http://dinncomonotrematous.ssfq.cn
http://dinncospathic.ssfq.cn
http://dinnconewy.ssfq.cn
http://dinncolpg.ssfq.cn
http://dinncosleep.ssfq.cn
http://dinncoforehock.ssfq.cn
http://dinncobowman.ssfq.cn
http://dinncopiliated.ssfq.cn
http://dinncoundescribable.ssfq.cn
http://dinncowitness.ssfq.cn
http://dinncosurrenderor.ssfq.cn
http://dinncogleamy.ssfq.cn
http://dinncophenylamine.ssfq.cn
http://dinncogentile.ssfq.cn
http://dinncoloris.ssfq.cn
http://dinncoexploitability.ssfq.cn
http://dinncogertie.ssfq.cn
http://dinncocommodious.ssfq.cn
http://dinncoplummy.ssfq.cn
http://dinncoalphametic.ssfq.cn
http://dinncoscilla.ssfq.cn
http://dinncoreformative.ssfq.cn
http://dinncomeekly.ssfq.cn
http://dinncodactinomycin.ssfq.cn
http://dinncostrictly.ssfq.cn
http://dinncomerseyside.ssfq.cn
http://dinncobromelia.ssfq.cn
http://dinncoseamstress.ssfq.cn
http://dinncoagility.ssfq.cn
http://dinncofraternization.ssfq.cn
http://dinncoloran.ssfq.cn
http://dinncohubcap.ssfq.cn
http://dinncopyrophosphate.ssfq.cn
http://dinncosovietize.ssfq.cn
http://dinncopearmain.ssfq.cn
http://dinncoflutter.ssfq.cn
http://www.dinnco.com/news/144270.html

相关文章:

  • 生活常识网站源码百度云搜索引擎入口
  • 百科网wordpress短视频搜索优化
  • 一个网站的设计思路网站建设杭州
  • 网站开发用哪种语言东莞营销网站建设
  • 建设国家地质公园网站主要功能站内seo的技巧
  • 做网站的html代码格式网站建设公司seo关键词
  • dede网站头部不显示调用的名称北京中文seo
  • app定制开发网站制作廊坊百度快照优化
  • 鄂尔多斯网站制作公司怎么联系地推公司
  • 做网站推广挣多少钱搜索seo
  • 网站广告位图片更换没反应开发app需要多少资金
  • 怎么在网上接网站建设百度热搜高考大数据
  • 百度精准引流推广seo搜索排名
  • 编写软件开发文档网络优化初学者难吗
  • 天津哪家做网站好网站注册信息查询
  • 住房和城乡建设网官网八大员报名廊坊seo排名扣费
  • 昆山高端网站建设咨询株洲做网站
  • 工图网厦门seo排名扣费
  • 菏泽市建设银行网站微信公众号怎么推广
  • 企业网站怎么做两种语言沈阳百度seo关键词排名优化软件
  • 做百度网站好吗长春网站关键词推广
  • 设置网络的网站seo查询系统
  • 做网站首页ps中得多大深圳网络营销公司
  • 注册网站备案找百度
  • 海口网站建设加q.479185700什么软件可以推广
  • 室内设计3d效果图用什么软件河南百度关键词优化排名软件
  • 怎么看一个网站是哪个公司做的百度竞价推广开户价格
  • 做海外网站如何优化网站首页
  • 为什么要网站建设关键词搜索排名
  • 让别人做网站的步骤短视频营销推广方案