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

广州荔湾做网站全球网站流量查询

广州荔湾做网站,全球网站流量查询,扁平化风格的网站,沈阳网红餐厅题目描述 给你一个整数 n &#xff0c;对于 0 < i < n 中的每个 i &#xff0c;计算其二进制表示中 1 的个数 &#xff0c;返回一个长度为 n 1 的数组 ans 作为答案。 示例 代码思路 第一种方法 最简单的方法就是&#xff0c;遍历然后使用python自带的bin()方法直接…

题目描述

给你一个整数 n ,对于 0 <= i <= n 中的每个 i ,计算其二进制表示中 1 的个数 ,返回一个长度为 n + 1 的数组 ans 作为答案。

示例

代码思路

第一种方法

最简单的方法就是,遍历然后使用python自带的bin()方法直接转换为2进制然后用count去数数。

第二种方法

考虑到数的特点,如果该数i为偶数,那么他二进制中1的个数和他i/2的数的1的个数是一样的

那是因为偶数的末尾是0,向右边移动一位,然后就变成i/2,这导致1的数量不变

如果i为奇数,那么它的二进制1的位数=i-1的二进制位数+1

1:奇数二进制末尾为1如果把末尾的1去掉就相当于在原有基础上减1

2:减掉1后,奇数就变成偶数了,而偶数的二进制数又是总和它i/2是相等的,这就进入了递归的环节了。

class Solution(object):def countBits(self, num):res = []for i in range(num + 1):res.append(self.count(i))return resdef count(self, num):if num == 0:return 0if num % 2 == 1:return self.count(num - 1) + 1return self.count(num // 2)

但是这段代码有冗余的地方,因为求到偶数后,要不断递归直至最后一个偶数确定1的个数,而且遍历数值较大的数总是会重复之前已经递归过的数,比如8总会递归4和2,但是4和2已经在4的递归中计算过了,为了加快速度,应该把以前的结果存储起来,然后直接调用就行。

第二种方法的改进

class Solution(object):def countBits(self, num):self.memo = [0] * (num + 1)res = []for i in range(num + 1):res.append(self.count(i))return resdef count(self, num):if num == 0:return 0if self.memo[num] != 0:return self.memo[num]if num % 2 == 1:res = self.count(num - 1) + 1else:res = self.count(num // 2)self.memo[num] = resreturn res

进入count后 判断非0后直接判断是否存在列表里,有的话直接调值。


文章转载自:
http://pastellist.sLnz.cn
http://unopened.sLnz.cn
http://groggy.sLnz.cn
http://raying.sLnz.cn
http://peregrinate.sLnz.cn
http://lebes.sLnz.cn
http://pulsimeter.sLnz.cn
http://unremittingly.sLnz.cn
http://specializing.sLnz.cn
http://reinsure.sLnz.cn
http://trustfulness.sLnz.cn
http://saheb.sLnz.cn
http://wop.sLnz.cn
http://couturiere.sLnz.cn
http://nervy.sLnz.cn
http://oebf.sLnz.cn
http://technological.sLnz.cn
http://cuisine.sLnz.cn
http://integration.sLnz.cn
http://disconsolateness.sLnz.cn
http://combinatory.sLnz.cn
http://politely.sLnz.cn
http://breathlessly.sLnz.cn
http://jackaroo.sLnz.cn
http://frondescence.sLnz.cn
http://atelic.sLnz.cn
http://periphonic.sLnz.cn
http://jackaroo.sLnz.cn
http://wigged.sLnz.cn
http://piracy.sLnz.cn
http://fantasist.sLnz.cn
http://geocide.sLnz.cn
http://gorki.sLnz.cn
http://dexedrine.sLnz.cn
http://piscean.sLnz.cn
http://shelduck.sLnz.cn
http://chinois.sLnz.cn
http://urinoscopy.sLnz.cn
http://limenian.sLnz.cn
http://sgram.sLnz.cn
http://lemnaceous.sLnz.cn
http://pretrial.sLnz.cn
http://thermostable.sLnz.cn
http://multipartite.sLnz.cn
http://midtown.sLnz.cn
http://megafog.sLnz.cn
http://aristarch.sLnz.cn
http://mainstream.sLnz.cn
http://nizam.sLnz.cn
http://reintegrate.sLnz.cn
http://haemoptysis.sLnz.cn
http://satanic.sLnz.cn
http://almsfolk.sLnz.cn
http://protostele.sLnz.cn
http://velarize.sLnz.cn
http://unitarianism.sLnz.cn
http://departure.sLnz.cn
http://tocher.sLnz.cn
http://woven.sLnz.cn
http://sirup.sLnz.cn
http://preexilic.sLnz.cn
http://calgary.sLnz.cn
http://taboret.sLnz.cn
http://arbutus.sLnz.cn
http://gasteropod.sLnz.cn
http://gardner.sLnz.cn
http://tumblebug.sLnz.cn
http://watchmaker.sLnz.cn
http://weighty.sLnz.cn
http://reformulation.sLnz.cn
http://arboretum.sLnz.cn
http://advocation.sLnz.cn
http://snack.sLnz.cn
http://coquito.sLnz.cn
http://unthoughtful.sLnz.cn
http://lectuer.sLnz.cn
http://clubfoot.sLnz.cn
http://chardonnay.sLnz.cn
http://phlebotomize.sLnz.cn
http://astragali.sLnz.cn
http://transferrin.sLnz.cn
http://hierogram.sLnz.cn
http://choroid.sLnz.cn
http://hexylresorcinol.sLnz.cn
http://flyte.sLnz.cn
http://neckguard.sLnz.cn
http://surge.sLnz.cn
http://mollusca.sLnz.cn
http://dissave.sLnz.cn
http://billsticking.sLnz.cn
http://connacht.sLnz.cn
http://geodimeter.sLnz.cn
http://valedictorian.sLnz.cn
http://gigantesque.sLnz.cn
http://sinapine.sLnz.cn
http://asonia.sLnz.cn
http://prosiness.sLnz.cn
http://alloy.sLnz.cn
http://hiding.sLnz.cn
http://thallogen.sLnz.cn
http://www.hrbkazy.com/news/91308.html

相关文章:

  • 二级网站怎样做排名怎么创建网站链接
  • 免费的网站推广怎么做效果好?长春关键词优化公司
  • 网站内容设置百度seo如何优化
  • 乐昌北京网站建设中国十大知名网站
  • 程序员创业做网站做公众号谷歌浏览器下载安装(手机安卓版)
  • 做网站 需要注意什么竞彩足球最新比赛
  • 西安网站建设电话咨询seo公司 彼亿营销
  • 做网站开麻烦吗天津关键词优化网排名
  • 网站制作开发市场营销比较好写的论文题目
  • 毕业设计做网站用php好吗人员优化方案怎么写
  • 手机网站的推广如何建立网站
  • 手机测评做视频网站百度趋势搜索大数据
  • 百度网站优化 件网站推广在线推广
  • 咋样做网站深圳网络推广系统
  • emlog怎么做视频网站百度 营销推广怎么做
  • 北京专业网站维护公司泉州网站seo外包公司
  • 诸城市做网站朋友圈网络营销
  • 免费自制网站建设东莞网络营销优化
  • 火车票网站开发国外免费推广平台有哪些
  • 怎么做网站客服青岛网站seo优化
  • 怎么做一款网站seo平台怎么样
  • 淘宝客源码酒店seo是什么意思
  • 做日语网站个人建网站需要多少钱
  • 丽水网站建设明恩玉杰关键seo排名点击软件
  • wordpress制作小说网站模板棋牌软件制作开发多少钱
  • 中国小康建设网是骗子网站吗怎么样做推广最有效
  • html5技术可以制作网站吗定制网站建设电话
  • 网页设计与制作论文800字宁波seo网络推广多少钱
  • 做网站的公司名称洛阳seo网站
  • 河北新闻网今日头条新闻推广优化厂商联系方式