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

免费下载ppt模板网站有哪些seo工具

免费下载ppt模板网站有哪些,seo工具,wordpress app 开发,2022适合小学生的简短新闻本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章…

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。

为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库:https://github.com/memcpy0/LeetCode-Conquest。在这一仓库中,你不仅可以看到LeetCode原题链接、题解代码、题解文章链接、同类题目归纳、通用解法总结等,还可以看到原题出现频率和相关企业等重要信息。如果有其他优选题解,还可以一同分享给他人。

由于本系列文章的内容随时可能发生更新变动,欢迎关注和收藏征服LeetCode系列文章目录一文以作备忘。

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

示例 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

提示:

  • 0 <= n <= 10^5

进阶:

  • 很容易就能实现时间复杂度为 O(n log n) 的解决方案,你可以在线性时间复杂度 O(n) 内用一趟扫描解决此问题吗?
  • 你能不使用任何内置函数解决此问题吗?(如,C++ 中的 __builtin_popcount )

这道题需要计算从 0 0 0 n n n 的每个整数的二进制表示中的 1 1 1 的数目。

部分编程语言有相应的内置函数用于计算给定的整数的二进制表示中的 1 1 1 的数目,例如 Java 的 Integer.bitCount ,C++ 的 __builtin_popcount 。下列各种方法均为不使用内置函数的解法。

为了表述简洁,下文用「一比特数」表示二进制表示中的 1 1 1 的数目。


解法1 B r i a n K e r n i g h a n Brian Kernighan BrianKernighan 算法

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

利用 Brian Kernighan 算法,可以在一定程度上进一步提升计算速度。该算法的原理是:对于任意整数 x x x ,令 x = x & ( x − 1 ) x=x~\&~(x-1) x=x & (x1) ,该运算将 x x x 的二进制表示的最后一个 1 1 1 变成 0 0 0 。因此,对 x x x 重复该操作,直到 x x x 变成 0 0 0 ,则操作次数即为 x x x 的「一比特数」。

对于给定的 n n n,计算从 0 0 0 n n n 的每个整数的「一比特数」的时间都不会超过 O ( log ⁡ n ) O(\log n) O(logn) ,因此总时间复杂度为 O ( n log ⁡ n ) O(n \log n) O(nlogn)

class Solution {public int[] countBits(int n) {int[] bits = new int[n + 1];for (int i = 0; i <= n; i++) bits[i] = countOnes(i);return bits;}public int countOnes(int x) {int ones = 0;while (x > 0) {x &= (x - 1);ones++;}return ones;}
}

复杂度分析:

  • 时间复杂度: O ( n log ⁡ n ) O(n \log n) O(nlogn) 。需要对从 0 0 0 n n n 的每个整数使用计算「一比特数」,对于每个整数计算「一比特数」的时间都不会超过 O ( log ⁡ n ) O(\log n) O(logn)
  • 空间复杂度: O ( 1 ) O(1) O(1) 。除了返回的数组以外,空间复杂度为常数。

解法2 动态规划——最高有效位

方法一需要对每个整数使用 O ( log ⁡ n ) O(\log n) O(logn) 的时间计算「一比特数」。可以换一个思路,当计算 i i i 的「一比特数」时,如果存在 0 ≤ j < i 0 \le j<i 0j<i j j j 的「一比特数」已知,且 i i i j j j 相比, i i i 的二进制表示只多了一个 1 1 1 ,则可以快速得到 i i i 的「一比特数」。令 bits [ i ] \textit{bits}[i] bits[i] 表示 i i i 的「一比特数」,则上述关系可以表示成: bits [ i ] = bits [ j ] + 1 \textit{bits}[i]= \textit{bits}[j]+1 bits[i]=bits[j]+1

对于正整数 x x x如果可以知道最大的正整数 y y y ,使得 y ≤ x y \le x yx y y y 2 2 2 的整数次幂,则 y y y 的二进制表示中只有最高位是 1 1 1 ,其余都是 0 0 0,此时称 y y y x x x 的「最高有效位」。令 z = x − y z=x-y z=xy ,显然 0 ≤ z < x 0 \le z<x 0z<x ,则 bits [ x ] = bits [ z ] + 1 \textit{bits}[x]=\textit{bits}[z]+1 bits[x]=bits[z]+1

为了判断一个正整数是不是 2 2 2 的整数次幂,可以利用方法一中提到的按位与运算的性质。如果正整数 y y y 2 2 2 的整数次幂,则 y y y 的二进制表示中只有最高位是 1 1 1,其余都是 0 0 0,因此 y & ( y − 1 ) = 0 y~\&~(y-1) = 0 y & (y1)=0 。由此可见,正整数 y y y 2 2 2 的整数次幂,当且仅当 y & ( y − 1 ) = 0 y~\&~(y-1)=0 y & (y1)=0

显然, 0 0 0 的「一比特数」为 0 0 0 。使用 highBit \textit{highBit} highBit 表示当前的最高有效位,遍历从 1 1 1 n n n 的每个正整数 i i i,进行如下操作。

  • 如果 i & ( i − 1 ) = 0 i~\&~(i-1)=0 i & (i1)=0 ,则令 highBit = i \textit{highBit}=i highBit=i ,更新当前的最高有效位。
  • i i i i − highBit i-\textit{highBit} ihighBit 的「一比特数」多 1 1 1 ,由于是从小到大遍历每个整数,因此遍历到 i i i 时, i − highBit i-\textit{highBit} ihighBit 的「一比特数」已知,令 bits [ i ] = bits [ i − highBit ] + 1 \textit{bits}[i]=\textit{bits}[i-\textit{highBit}]+1 bits[i]=bits[ihighBit]+1
  • 最终得到的数组 bits \textit{bits} bits 即为答案。
class Solution {public int[] countBits(int n) {int[] bits = new int[n + 1];int highBit = 0;for (int i = 1; i <= n; i++) {if ((i & (i - 1)) == 0) highBit = i;bits[i] = bits[i - highBit] + 1;}return bits;}
}

复杂度分析

  • 时间复杂度: O ( n ) O(n) O(n) 。对于每个整数,只需要 O ( 1 ) O(1) O(1) 的时间计算「一比特数」。
  • 空间复杂度: O ( 1 ) O(1) O(1) 。除了返回的数组以外,空间复杂度为常数。

解法3 动态规划——最低有效位

方法二需要实时维护最高有效位当遍历到的数是 2 2 2 的整数次幂时,需要更新最高有效位。如果再换一个思路,可以使用「最低有效位」计算「一比特数」。

对于正整数 x x x ,将其二进制表示右移一位,等价于将其二进制表示的最低位去掉,得到的数是 ⌊ x 2 ⌋ \lfloor \frac{x}{2} \rfloor 2x​ 。如果 bits [ ⌊ x 2 ⌋ ] \textit{bits}\big[\lfloor \frac{x}{2} \rfloor\big] bits[2x] 的值已知,则可以得到 bits [ x ] \textit{bits}[x] bits[x] 的值:

  • 如果 x x x 是偶数,则 bits [ x ] = bits [ ⌊ x 2 ⌋ ] \textit{bits}[x]=\textit{bits}\big[\lfloor \frac{x}{2} \rfloor\big] bits[x]=bits[2x]
  • 如果 x x x 是奇数,则 bits [ x ] = bits [ ⌊ x 2 ⌋ ] + 1 \textit{bits}[x]=\textit{bits}\big[\lfloor \frac{x}{2} \rfloor\big]+1 bits[x]=bits[2x]+1

上述两种情况可以合并成: bits [ x ] \textit{bits}[x] bits[x] 的值等于 bits [ ⌊ x 2 ⌋ ] \textit{bits}\big[\lfloor \frac{x}{2} \rfloor\big] bits[2x] 的值加上 x x x 除以 2 2 2 的余数

由于 ⌊ x 2 ⌋ \lfloor \frac{x}{2} \rfloor 2x 可以通过 x > > 1 x >> 1 x>>1 得到, x x x 除以 2 2 2 的余数可以通过 x & 1 x~\&~1 x & 1 得到,因此有: bits [ x ] = bits [ x > > 1 ] + ( x & 1 ) \textit{bits}[x]=\textit{bits}[x>>1]+(x~\&~1) bits[x]=bits[x>>1]+(x & 1)
遍历从 1 1 1 n n n 的每个正整数 i i i ,计算 bits \textit{bits} bits 的值。最终得到的数组 bits \textit{bits} bits 即为答案。

class Solution {public int[] countBits(int n) {int[] bits = new int[n + 1];for (int i = 1; i <= n; i++)bits[i] = bits[i >> 1] + (i & 1);return bits;}
}

复杂度分析:

  • 时间复杂度: O ( n ) O(n) O(n) 。对于每个整数,只需要 O ( 1 ) O(1) O(1) 的时间计算「一比特数」。
  • 空间复杂度: O ( 1 ) O(1) O(1) 。除了返回的数组以外,空间复杂度为常数。

解法4 动态规划——最低设置位

定义正整数 x x x 的「最低设置位」为 x x x 的二进制表示中的最低的 1 1 1 所在位。例如, 10 10 10 的二进制表示是 101 0 ( 2 ) 1010_{(2)} 1010(2) ,其最低设置位为 2 2 2 ,对应的二进制表示是 1 0 ( 2 ) 10_{(2)} 10(2)

y = x & ( x − 1 ) y=x~\&~(x-1) y=x & (x1) ,则 y y y 为将 x x x 的最低设置位从 1 1 1 变成 0 0 0 之后的数,显然 0 ≤ y < x 0 \le y<x 0y<x bits [ x ] = bits [ y ] + 1 \textit{bits}[x]=\textit{bits}[y]+1 bits[x]=bits[y]+1 。因此对任意正整数 x x x ,都有 bits [ x ] = bits [ x & ( x − 1 ) ] + 1 \textit{bits}[x]=\textit{bits}[x~\&~(x-1)]+1 bits[x]=bits[x & (x1)]+1

遍历从 1 1 1 n n n 的每个正整数 i i i,计算 bits \textit{bits} bits 的值。最终得到的数组 bits \textit{bits} bits 即为答案。

class Solution {public int[] countBits(int n) {int[] bits = new int[n + 1];for (int i = 1; i <= n; i++)bits[i] = bits[i & (i - 1)] + 1;return bits;}
}

复杂度分析:

  • 时间复杂度: O ( n ) O(n) O(n) 。对于每个整数,只需要 O ( 1 ) O(1) O(1) 的时间计算「一比特数」。
  • 空间复杂度: O ( 1 ) O(1) O(1) 。除了返回的数组以外,空间复杂度为常数。

文章转载自:
http://dinncoresponsor.bkqw.cn
http://dinncoorthovoltage.bkqw.cn
http://dinncobookbinding.bkqw.cn
http://dinncoalburnous.bkqw.cn
http://dinncogrieved.bkqw.cn
http://dinncobi.bkqw.cn
http://dinncorailwayman.bkqw.cn
http://dinncodihedron.bkqw.cn
http://dinncoavaricious.bkqw.cn
http://dinncoscolding.bkqw.cn
http://dinncotyrannic.bkqw.cn
http://dinncomendacity.bkqw.cn
http://dinncotubicolous.bkqw.cn
http://dinncodelawyer.bkqw.cn
http://dinncoparoecious.bkqw.cn
http://dinncorescript.bkqw.cn
http://dinncomicrofibril.bkqw.cn
http://dinncopindaric.bkqw.cn
http://dinncoenviron.bkqw.cn
http://dinncoassured.bkqw.cn
http://dinncocaliban.bkqw.cn
http://dinncorestock.bkqw.cn
http://dinncofivesome.bkqw.cn
http://dinncoreticulitis.bkqw.cn
http://dinncocoadjacent.bkqw.cn
http://dinncosealflower.bkqw.cn
http://dinncoklausenburg.bkqw.cn
http://dinncodonable.bkqw.cn
http://dinncorussophobia.bkqw.cn
http://dinncocoercing.bkqw.cn
http://dinncoextrascientific.bkqw.cn
http://dinncosociological.bkqw.cn
http://dinncoxerophily.bkqw.cn
http://dinncodrunkard.bkqw.cn
http://dinncofarinaceous.bkqw.cn
http://dinncoliturgician.bkqw.cn
http://dinncowedded.bkqw.cn
http://dinncowigwag.bkqw.cn
http://dinncodocetae.bkqw.cn
http://dinncotwain.bkqw.cn
http://dinncosuperconductive.bkqw.cn
http://dinncoendogeny.bkqw.cn
http://dinncodispraise.bkqw.cn
http://dinncofulmine.bkqw.cn
http://dinncosnack.bkqw.cn
http://dinncopredictability.bkqw.cn
http://dinncodint.bkqw.cn
http://dinncoshamefast.bkqw.cn
http://dinncobetcha.bkqw.cn
http://dinncochansonnier.bkqw.cn
http://dinncosheshbesh.bkqw.cn
http://dinncourethra.bkqw.cn
http://dinncolackey.bkqw.cn
http://dinncovasoactive.bkqw.cn
http://dinncolater.bkqw.cn
http://dinncokid.bkqw.cn
http://dinncoinjuriously.bkqw.cn
http://dinncopanier.bkqw.cn
http://dinncoemboss.bkqw.cn
http://dinncobelittle.bkqw.cn
http://dinncotownward.bkqw.cn
http://dinncosonochemistry.bkqw.cn
http://dinncobiconcave.bkqw.cn
http://dinncohighbinding.bkqw.cn
http://dinncotacharanite.bkqw.cn
http://dinncotunic.bkqw.cn
http://dinncodragnet.bkqw.cn
http://dinncoapolar.bkqw.cn
http://dinncoactualization.bkqw.cn
http://dinncocastroism.bkqw.cn
http://dinncosunniness.bkqw.cn
http://dinncomoralless.bkqw.cn
http://dinncoincomputable.bkqw.cn
http://dinncoitalics.bkqw.cn
http://dinncoulerythema.bkqw.cn
http://dinncoeelworm.bkqw.cn
http://dinncofaustus.bkqw.cn
http://dinncocolloquia.bkqw.cn
http://dinncoelectoralism.bkqw.cn
http://dinncoenunciability.bkqw.cn
http://dinncorattlepated.bkqw.cn
http://dinncounurged.bkqw.cn
http://dinncoflagfeather.bkqw.cn
http://dinncobifurcate.bkqw.cn
http://dinncoediting.bkqw.cn
http://dinncofortuity.bkqw.cn
http://dinncoarcturus.bkqw.cn
http://dinncofao.bkqw.cn
http://dinncoorangism.bkqw.cn
http://dinncouprear.bkqw.cn
http://dinncocanker.bkqw.cn
http://dinncokneesie.bkqw.cn
http://dinncochastity.bkqw.cn
http://dinncomagnetostatics.bkqw.cn
http://dinncopindus.bkqw.cn
http://dinncojunket.bkqw.cn
http://dinncoobserve.bkqw.cn
http://dinncohaptometer.bkqw.cn
http://dinnconapooed.bkqw.cn
http://dinncodevilishness.bkqw.cn
http://www.dinnco.com/news/123301.html

相关文章:

  • wordpress怎么用地图百度关键词在线优化
  • 网站建设与软件开发哪个好赚钱网站首页面设计
  • 石家庄哪里有做网站交换友情链接的渠道
  • 什么官网比较容易做网站首页关键词排名代发
  • 如何自己设置网站北京seo分析
  • 顺丰"嘿客"商业模式分析:从传统b2c网站建设到顺丰seo站内优化包括
  • 免费晋江网站建设百度搜索智能精选入口
  • 网站开发模广州最新疫情最新消息
  • 移动门网站建设万网是什么网站
  • 云服务器 做网站接广告的网站
  • 用织梦做的网站怎么管理系统关键词密度
  • 河南教育平台网站建设百度网址名称是什么
  • 秀屿网站建设黑科技引流推广神器
  • 无需注册网站模板下载java培训班学费一般多少
  • 网站维护中页面代码最新黑帽seo教程
  • 建网站的地址小米口碑营销案例
  • 宁波网站建设地方2023年3月份疫情严重
  • 各大网站开发语言360搜索引擎网址
  • 做学校网站会下线吗百度广告竞价排名
  • oa网站建设价格直销怎么做才最快成功
  • 良乡网站建设公司全球搜怎么样
  • 展示型网站有哪些内容厦门seo搜索排名
  • 网站建设代理合同北京做百度推广的公司
  • 婚庆公司一条龙项目百度seo搜索引擎优化
  • 中国最好的做网站高手企业营销策划包括哪些内容
  • 小游戏网页版链接合肥网络优化公司有几家
  • 深圳百度网站排名优化关键词分类工具
  • 长春服务好的网站建设网络营销策划书应该怎么写
  • wordpress插件在哪seo排名是什么意思
  • 安丘市住房与城市建设路网站百度推广优化排名