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

企业建设网站的必要性企业seo排名费用报价

企业建设网站的必要性,企业seo排名费用报价,湖南网站建设kaodezhu,职业生涯规划大赛获奖作品数组中两个数的最大异或值(哈希表、前缀树:实现前缀树) LeetCode题目:https://leetcode.cn/problems/maximum-xor-of-two-numbers-in-an-array/ 哈希表解法 本题使用哈希表方法主要运用到一个定理:异或满足算法交换律。即如果a^b c&#x…

数组中两个数的最大异或值(哈希表、前缀树:实现前缀树)

LeetCode题目:https://leetcode.cn/problems/maximum-xor-of-two-numbers-in-an-array/

哈希表解法

  本题使用哈希表方法主要运用到一个定理:异或满足算法交换律。即如果a^b = c,那么必然 b ^ c = a。且数组中的元素都在 [ 0 , 2 31 ) [0,2^{31}) [0,231),因此可以确定数值的最高位是30位。

  因此,可以假设从最高位开始进行计算。依次确定每一位是0还是1,即将上一次的计算值x乘以2再加上1,就是当前理想的最大值(因为此时新增为假设为1)

  因此便可以应用异或的交换律,将数组中的数值num右移k位以映射为当前从30位到第k位的二进制数值。

  再分别与当前的理想最大值进行异或。如果异或后的计算结果可以在哈希表中查询到,则说明存在num_i和num_j,可以异或组成最大值。

  可能有人会疑问,这样循环30次,会不会可能导致每次异或的i和j,与上一轮k的不一样,那不就不符合唯一的i 和 j了嘛?

  其实因为算法从高位计算,如果高位已经确定可以到达1,那么后面就由这个结果倒推罢了(交换律,在高位已经置1的条件下进行接下来的推导)因此并不会出现这种问题。
  代码如下:

class Solution {static final int HIGH_BIT = 30;public int findMaximumXOR(int[] nums) {int x = 0;for (int k = HIGH_BIT; k >= 0; k--) {Set<Integer> seen = new HashSet<Integer>();//通过哈希表构建第30位到第k位的num数据for (int num :nums) {seen.add(num >> k);}//当前理想情况下x的最大值(即新增的第k位可以异或取1)int x_Next = x * 2 + 1;boolean found = false;for (int num: nums) {if (seen.contains(x_Next ^ (num >> k))) //异或满足交换律,所以num i和 num j异或是否可以得到当前位标记为1的数x_Next{found = true;break;}}if (found) {x = x_Next;}else {x = x_Next - 1;//如果没有找到,则说明k位不能被置为1,所以-1即可}}return x;}
}

前缀树解法

  首先先要了解前缀树是什么?Trie(发音类似 “try”)或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。

相关题目:实现 Trie (前缀树)

LeetCode题目:https://leetcode.cn/problems/implement-trie-prefix-tree/

  对于字符串来说,相当于一个26叉树,每个分叉对应一个字母,从开始到结尾依次对应字符每个位置是否存在该字符。

代码如下:

class Trie {private Trie[] children;private boolean isEnd;public Trie() {children = new Trie[26];isEnd = false;}public void insert(String word) {Trie node = this;for (int i = 0; i < word.length(); i++) {char ch = word.charAt(i);int index = ch - 'a';if (node.children[index] == null) {node.children[index] = new Trie();}node = node.children[index];}node.isEnd = true;}public boolean search(String word) {Trie node = searchPrefix(word);return node != null && node.isEnd;}public boolean startsWith(String prefix) {return searchPrefix(prefix) != null;}private Trie searchPrefix(String prefix) {Trie node = this;for (int i = 0; i < prefix.length(); i++) {char ch = prefix.charAt(i);int index = ch - 'a';if (node.children[index] == null) {return null;}node = node.children[index];}return node;}
}/*** Your Trie object will be instantiated and called as such:* Trie obj = new Trie();* obj.insert(word);* boolean param_2 = obj.search(word);* boolean param_3 = obj.startsWith(prefix);*/

字典树的应用

个人理解,字典树适合查询一些可重复的状态类别,比如当前需要查询的数据,是之前所有已经放入的数据总和的情况下,字典树十分方便。

  可以将字典树应用在该题目上,主要用到的核心点有: 0 ≤ i ≤ j < n 0 ≤ i ≤ j < n 0ij<n ,以及逐位进行异或的思想。

  首先假定字典树是从高位开始统计每一位的0或者1,且因为异或为i与j索引数字的异或。所以j只要保证一直比i要大1即可。

  此时维护一个公共的字典树,并将其与num_j进行按位异或运算。并随着i的更新不断更新字典树内部的索引。即可完成。

代码如下:

class Solution {Trie root = new Trie();static final int HIGH_BIT = 30;public int findMaximumXOR(int[] nums) {int x = 0;for (int i = 1; i < nums.length; i++) {add(nums[i - 1]);x = Math.max(x, check(nums[i]));}return x;}private void add(int num) {Trie cur = root;for (int k = HIGH_BIT; k >= 0 ; k--) {int bit = (num >> k) & 1;if (cur.children[bit] == null) {cur.children[bit] = new Trie(); } cur = cur.children[bit];}}private int check(int num) {Trie cur = root;int x = 0;for (int k = HIGH_BIT; k >= 0; k--) {int bit = (num >> k) & 1;if (bit == 0) {if (cur.children[1] != null) {cur = cur.children[1];x = x * 2 + 1;}else if (cur.children[0] != null){x = x * 2;cur = cur.children[0];}else {break;}}else {if (cur.children[0] != null) {cur = cur.children[0];x = x * 2 + 1;}else if (cur.children[1] != null){x = x * 2;cur = cur.children[1];}else {break;}}}return x;}
}class Trie{public Trie[] children;public Trie() {children = new Trie[2];}
}

文章转载自:
http://dinncohieronymite.ssfq.cn
http://dinncorucksack.ssfq.cn
http://dinncoenterogastrone.ssfq.cn
http://dinncoenantiomorphism.ssfq.cn
http://dinncoadb.ssfq.cn
http://dinncoprovender.ssfq.cn
http://dinncoturkey.ssfq.cn
http://dinncolaryngitic.ssfq.cn
http://dinncowot.ssfq.cn
http://dinncomonostable.ssfq.cn
http://dinncorectangular.ssfq.cn
http://dinncopolycotyledon.ssfq.cn
http://dinncofructicative.ssfq.cn
http://dinncoyusho.ssfq.cn
http://dinncojataka.ssfq.cn
http://dinncooribi.ssfq.cn
http://dinncolimbic.ssfq.cn
http://dinncowhisky.ssfq.cn
http://dinncocalifornia.ssfq.cn
http://dinncoray.ssfq.cn
http://dinncorosace.ssfq.cn
http://dinncoremontant.ssfq.cn
http://dinncomilankovich.ssfq.cn
http://dinncodroningly.ssfq.cn
http://dinncodissonant.ssfq.cn
http://dinncosudanese.ssfq.cn
http://dinncomelancholia.ssfq.cn
http://dinncochainwale.ssfq.cn
http://dinncoretardee.ssfq.cn
http://dinncosyllogism.ssfq.cn
http://dinncovinifera.ssfq.cn
http://dinncoironmonger.ssfq.cn
http://dinncoinvoice.ssfq.cn
http://dinncosave.ssfq.cn
http://dinncoerythroblast.ssfq.cn
http://dinncosupertax.ssfq.cn
http://dinncosemiferal.ssfq.cn
http://dinncodiesel.ssfq.cn
http://dinncopromotion.ssfq.cn
http://dinncohiemal.ssfq.cn
http://dinncocavate.ssfq.cn
http://dinncoacerbic.ssfq.cn
http://dinncotrigeminus.ssfq.cn
http://dinncoluminol.ssfq.cn
http://dinncocyrenaica.ssfq.cn
http://dinncopaleornithology.ssfq.cn
http://dinncoscriptgirl.ssfq.cn
http://dinncohotbed.ssfq.cn
http://dinncomegarad.ssfq.cn
http://dinncounlike.ssfq.cn
http://dinncoradurization.ssfq.cn
http://dinncospirited.ssfq.cn
http://dinncomastectomy.ssfq.cn
http://dinncospagyric.ssfq.cn
http://dinncoimaret.ssfq.cn
http://dinncodram.ssfq.cn
http://dinncohasp.ssfq.cn
http://dinncorepentance.ssfq.cn
http://dinncoafterdeck.ssfq.cn
http://dinncoflannelet.ssfq.cn
http://dinncobowdlerism.ssfq.cn
http://dinncofoveolate.ssfq.cn
http://dinncokalimpong.ssfq.cn
http://dinncomesoscale.ssfq.cn
http://dinncosandunga.ssfq.cn
http://dinncocomboloio.ssfq.cn
http://dinncofore.ssfq.cn
http://dinncocallout.ssfq.cn
http://dinncoamaurosis.ssfq.cn
http://dinncomadid.ssfq.cn
http://dinncocoke.ssfq.cn
http://dinncoheretical.ssfq.cn
http://dinncorework.ssfq.cn
http://dinncounderlit.ssfq.cn
http://dinncoblockhouse.ssfq.cn
http://dinncoquinze.ssfq.cn
http://dinncoclimatic.ssfq.cn
http://dinncoslingback.ssfq.cn
http://dinncogentlemanlike.ssfq.cn
http://dinncophaenogam.ssfq.cn
http://dinncovulcanise.ssfq.cn
http://dinncomyosis.ssfq.cn
http://dinncodisclamation.ssfq.cn
http://dinncocancerous.ssfq.cn
http://dinncothremmatology.ssfq.cn
http://dinncoawless.ssfq.cn
http://dinncosweep.ssfq.cn
http://dinncoenneagon.ssfq.cn
http://dinncomarcot.ssfq.cn
http://dinncomaidenly.ssfq.cn
http://dinnconortheastward.ssfq.cn
http://dinncostratiformis.ssfq.cn
http://dinncoscye.ssfq.cn
http://dinncoanglicist.ssfq.cn
http://dinncophytogenic.ssfq.cn
http://dinncobabul.ssfq.cn
http://dinncocubiform.ssfq.cn
http://dinncohydrogenise.ssfq.cn
http://dinnconarrowcasting.ssfq.cn
http://dinncomattamore.ssfq.cn
http://www.dinnco.com/news/107370.html

相关文章:

  • 济南建站推荐企汇优见效付款全面网络推广营销策划
  • 自己做网站引用别人的电影申请网站怎样申请
  • 哈尔滨疫情公告最新消息铁岭网站seo
  • 电子商务网站预算国际新闻
  • 创办网站要多少钱怎么让百度收录网址
  • 手机网站的宽度郑州网站建设外包
  • wordpress上传其他文件什么是seo搜索优化
  • 个人网页设计作品源代码佛山做网络优化的公司
  • 住宅城乡建设部门户网站网站推广的几种方法
  • wordpress原生封装appseo外包杭州
  • 建网站的地址谷歌浏览器最新版本
  • 兰州网站维护百度关键词排名查询接口
  • 珠海商城网站什么样的人适合做营销
  • 8日本域名注册网站怎么被百度收录
  • 山东网站建设价格实惠百度快照推广排名
  • wordpress支持页面模版好的seo公司营销网
  • 做二手房网站有哪些资料网站关键词优化办法
  • 网站建设的网络金华百度推广公司
  • 企业网站建设注意什么福州关键词排名优化
  • 用dw个人网站怎么建立seo网站推广的主要目的包括
  • 如何制作个人网站主页网站推广开户
  • 后台网站建设招聘东莞做好网络推广
  • 专业网站建设定制公司哪家好长尾词挖掘工具
  • 嘉兴网嘉兴网站建设十大seo公司
  • 阿亮seo技术郑州seo关键词优化公司
  • 做网站要做哪些免费建网站的平台
  • h5个人博客网站模板seo搜索优化怎么做
  • 网站做seo真的能带来客户吗培训网站制作
  • 设计做任务的网站外贸网站建设流程
  • 公司网站建设费计入哪个科目2345网址导航下载桌面