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

wordpress配置教程seo优化顾问服务

wordpress配置教程,seo优化顾问服务,建一个个人网站要多少钱,建行个人网上银行登录入口目录 一,常见位运算操作总结二,算法原理和代码实现191.位1的个数338.比特位计数461.汉明距离面试题01.01.判断字符是否唯一268.丢失的数字371.两整数之和136.只出现一次的数字137.只出现一次的数字II260.只出现一次的数据III面试题17.19.消失的两个数字 …

目录

  • 一,常见位运算操作总结
  • 二,算法原理和代码实现
    • 191.位1的个数
    • 338.比特位计数
    • 461.汉明距离
    • 面试题01.01.判断字符是否唯一
    • 268.丢失的数字
    • 371.两整数之和
    • 136.只出现一次的数字
    • 137.只出现一次的数字II
    • 260.只出现一次的数据III
    • 面试题17.19.消失的两个数字
  • 三,算法总结

一,常见位运算操作总结

1. 基础位运算符

**(1) 左移:<<**
**(2) 右移:>>**
**(3) 取反:~**
**(4) 按位与:& -- ==有 0 就是 0==**
**(5) 按位或:| -- ==有 1 就是1==**
**(6) 按位异或:^ -- ==相同为0,相异为1/无进位相加==**

注意:参与位运算的对象只能是整型数据(int, unsigned, char),不能为实型

上面六种基础位运算是本篇文章重点涉及的,要想详细了解它们的含义和运算规律,请点击文章:【移位操作符,位操作符运算规则详解】

2. 位运算符的优先级

在这里插入图片描述

只要记住一句话:表格不用死记,能加括号就加括号

3. 给定一个数 n ,判断他的二进制表示的第 x 位是 0 还是 1?

(n >> x) & 1

4. 将一个数 n 的二进制表示的第 x 位修改成 1

n |= (1 << x)

5. 将一个数 n 的二进制表示的第 x 位修改成 0

n &= (~(1 << x))

6. 位图思想

位图的本质是哈希表,是一个用二进制比特位表示数据是否存在的数据结构

想详细了解什么是位图以及位图的使用,请点击文章:【哈希的应用 – 位图&布隆过滤器】

7. 提取一个数(n)二进制表示中最右侧的 1

n & -n

在这里插入图片描述

8. 干掉一个数(n)二进制表示中最右侧的 1

n & (n - 1)

在这里插入图片描述

9. 异或(^)运算的运算律

在这里插入图片描述

二,算法原理和代码实现

191.位1的个数

在这里插入图片描述
在这里插入图片描述
算法原理:
根据上面总结的位运算的操作,这道题有两种解法

代码实现1:

根据上面的第三点,可以判断 n 的二进制里的每一位是否是1,如果是,计数器++

class Solution
{
public:int hammingWeight(int n){int count = 0;for (int i = 0; i < 32; i++){if ((n >> i) & 1) count++;}return count;}
};

代码实现2:

根据上面的第8点,每次都干掉数 n 的最右侧的1,统计执行的次数即可

class Solution {
public:int hammingWeight(int n) {int count = 0;while (n){n &= (n - 1);count++;}return count;}
};

338.比特位计数

在这里插入图片描述
在这里插入图片描述

算法原理:

这道题就是上一题的进阶题,算法原理同上,加一个循环遍历从0到n的数字,分别计算出每个数字的二进制中1的个数,存入数组中即可

代码实现:

class Solution 
{
public:vector<int> countBits(int n) {vector<int> ret;for(int i = 0; i <= n; i++){int tmp = i;unsigned int count = 0;while(tmp){tmp &= (tmp-1);count++;}ret.push_back(count);}return ret;}
};

461.汉明距离

在这里插入图片描述
在这里插入图片描述

算法原理:

根据上面的操作三,判断这两个数的每一位是否相等,如果不相等,计数器++ 即可

代码实现:

class Solution 
{
public:int hammingDistance(int x, int y) {int i = 0;int count = 0;for(; i < 32; i++)if(((x >> i) & 1) != ((y >> i) & 1)) count++;return count;}
};

面试题01.01.判断字符是否唯一

在这里插入图片描述
在这里插入图片描述

算法原理:
这道题比较简单,有多种解法:一是使用位图思想,二是使用哈希表,三是用数组模拟哈希

这里介绍位图思想

用一个 int 变量的32个比特位(实际上只使用26个)来标记这个字符在或不在,0 表示不在,1 表示在
所以先要判断二进制中的第 n 位是 1 还是 0,如果是 0,就修改成 1,如果已经是 1 了,说明这个字符就已经存在了,返回false
在这里插入图片描述

这里还有一个小的优化点:

由鸽巢原理(抽屉原理)可知,当字符串的长度大于26个时,一定会出现重复字符

代码实现1:使用位图思想

class Solution
{
public:bool isUnique(string astr){if(astr.size() > 26) return false;int bitMap = 0;for (auto ch : astr){int i = ch - 'a'; // 移动的位数// 字符不存在,映射位置的比特位修改成1if (((bitMap >> i) & 1) == 0) bitMap |= (1 << i);else return false;}return true;}
};

代码实现2:使用哈希表
时间复杂度:O(N)
空间复杂度:O(N)

class Solution
{
public:bool isUnique(string astr){if(astr.size() > 26) return false;unordered_map<char, int> hash;for (auto ch : astr){// 判断是否存在if (hash.count(ch) == 0) hash[ch]++;else return false;}return true;}
};

代码实现3:用数组模拟哈希

class Solution
{
public:bool isUnique(string astr){if(astr.size() > 26) return false;int hash[26] = { 0 };for (auto ch : astr){if (hash[ch - 'a'] == 0) hash[ch - 'a']++;else return false;}return true;}
};

268.丢失的数字

在这里插入图片描述
在这里插入图片描述

算法原理:
这道题其实和 [二分查找] 系列中的最后一题是一模一样的,题目简单,解法多种

这里介绍使用位运算

使用异或运算的规律
a ^ 0 = a
a ^ a = 0
a ^ b ^ c = a ^ (b ^ c)
可以先把完整的 [0, n] 共 n + 1 个数进行异或,再把这个异或结果异或上题目所给的数据,相同数异或变成0,最后剩下的那个就是缺失的数字

代码实现:

class Solution 
{
public:int missingNumber(vector<int>& nums) {int ret = 0;for(int i = 0; i <= nums.size(); i++)ret ^= i;for(auto x : nums)ret ^= x;return ret;}
};

如果想要了解其他解法,请点击文章:【二分查找】里的最后一题 [LCR173.点名]

371.两整数之和

在这里插入图片描述
在这里插入图片描述

算法原理:

这道题使用异或运算 – 无进位相加
先算出无进位相加的结果,再算出进位,再把两者相加,但是不能使用加法,要重复执行上面两个操作,直到进位为 0 为止
在这里插入图片描述

代码实现:

class Solution 
{
public:int getSum(int a, int b) {int tmp = a ^ b; // 无进位相加的结果int res = (a & b) << 1; // 算出进位// 当进位不为0时,重复上面操作while(res){a = tmp;b = res;tmp = a ^ b;res = (a & b) << 1;}return tmp;}
};

136.只出现一次的数字

在这里插入图片描述
在这里插入图片描述

算法原理:

这道题很简单,就是对异或运算律(a ^ a = 0)的简单使用

代码实现:

class Solution 
{
public:int singleNumber(vector<int>& nums) {int ret = 0;for(auto x : nums)ret ^= x;return ret;}
};

137.只出现一次的数字II

在这里插入图片描述
在这里插入图片描述

算法原理:

把所有数据的第 i 个二进制位相加,和会出现下面4种情况
在这里插入图片描述
用相加的和取模3(%3),如果是三个相同的数,它们的第 i 个二进制位相加结果模3为 0,如果等于 1,说明这个二进制位是那个只出现一次的数的,就把它映射到另一个 int 变量的对应的二进制位上

代码实现:

class Solution 
{
public:int singleNumber(vector<int>& nums) {int ret = 0;for(int i = 0; i < 32; i++){int sum = 0;for(auto x : nums)sum += ((x >> i) & 1); // 把所有数字的第i个二进制位相加// 如果等于1,说明是出现一次的,把1映射到相应的二进制位if(sum % 3 != 0) ret |= (1 << i);  }return ret;}
};

260.只出现一次的数据III

在这里插入图片描述
在这里插入图片描述

算法原理:

其实根据 [136.只出现一次的数字] 对这道题是有思路的,就是相同的数异或在一起就变成0了,关键就是如何把相同的数放到一组?
在这里插入图片描述

代码实现:

class Solution 
{
public:vector<int> singleNumber(vector<int>& nums) {int tmp = 0;// 把所有数据异或在一起for(auto x : nums)tmp ^= x;// 找出tmp中,比特位上为1的位置int i = 0;for(; i < 32; i++)if((tmp >> i) & 1) break;// 根据i的位置把数据分成两类,分别异或int a = 0, b = 0;for(auto x : nums)if((x >> i) & 1) a ^= x;else b ^= x;return {a, b};}
};

面试题17.19.消失的两个数字

在这里插入图片描述
在这里插入图片描述

算法原理:
这道题本质上是 [268.消失的数字] 和 [260.只出现一次的数字III] 的结合题
在这里插入图片描述

代码实现:

class Solution 
{
public:vector<int> missingTwo(vector<int>& nums) {int tmp = 0;// 把所属异或在一起for(auto x : nums) tmp ^= x;for(int k = 1; k <= nums.size()+2; k++) tmp ^= k;// 找到tmp中,比特位上为1的那一位int i = 0;for(; i < 32; i++)if((tmp >> i) & 1) break;// 根据第i位的不同,把所有数据分成两类int a = 0, b = 0;for(auto x : nums)if((x >> i) & 1) a ^= x;else b ^= x;for(int j = 1 ; j <= nums.size()+2; j++)if((j >> i) & 1) a ^= j;else b ^= j;return {a, b};}
};

三,算法总结

熟练使用位运算的操作即可


文章转载自:
http://dinncoevenings.ssfq.cn
http://dinncobatteau.ssfq.cn
http://dinncodragonnade.ssfq.cn
http://dinncointerpellant.ssfq.cn
http://dinncohypothecate.ssfq.cn
http://dinncoformula.ssfq.cn
http://dinncolengthily.ssfq.cn
http://dinncoseral.ssfq.cn
http://dinncomillcake.ssfq.cn
http://dinncolassitude.ssfq.cn
http://dinncoencephalasthenia.ssfq.cn
http://dinncocandie.ssfq.cn
http://dinncocornland.ssfq.cn
http://dinncohaemolysis.ssfq.cn
http://dinncoputridity.ssfq.cn
http://dinncocede.ssfq.cn
http://dinncotartary.ssfq.cn
http://dinncocomplier.ssfq.cn
http://dinncogoatling.ssfq.cn
http://dinncoramdac.ssfq.cn
http://dinncoessex.ssfq.cn
http://dinncoextralunar.ssfq.cn
http://dinncointerlunar.ssfq.cn
http://dinncotelfordize.ssfq.cn
http://dinncopotbellied.ssfq.cn
http://dinncoblouse.ssfq.cn
http://dinncohypergraph.ssfq.cn
http://dinncofilterable.ssfq.cn
http://dinncobiferous.ssfq.cn
http://dinncokyongsong.ssfq.cn
http://dinncoexaminationism.ssfq.cn
http://dinncoportfolio.ssfq.cn
http://dinncomurein.ssfq.cn
http://dinncosunlit.ssfq.cn
http://dinncomonostele.ssfq.cn
http://dinncocostectomy.ssfq.cn
http://dinncoremarkably.ssfq.cn
http://dinncorisible.ssfq.cn
http://dinncoredefect.ssfq.cn
http://dinncocaliban.ssfq.cn
http://dinncopiccanin.ssfq.cn
http://dinncoplaywright.ssfq.cn
http://dinncolozengy.ssfq.cn
http://dinncostraggly.ssfq.cn
http://dinncocup.ssfq.cn
http://dinncocord.ssfq.cn
http://dinncoithun.ssfq.cn
http://dinncogoby.ssfq.cn
http://dinncofugacity.ssfq.cn
http://dinncoteague.ssfq.cn
http://dinncobottleholder.ssfq.cn
http://dinncobritisher.ssfq.cn
http://dinncoantepartum.ssfq.cn
http://dinncogobbledegook.ssfq.cn
http://dinncosnobby.ssfq.cn
http://dinncolumumbist.ssfq.cn
http://dinncogoosefoot.ssfq.cn
http://dinncoappellatively.ssfq.cn
http://dinncohundred.ssfq.cn
http://dinncoelevenses.ssfq.cn
http://dinncodeficit.ssfq.cn
http://dinncorobust.ssfq.cn
http://dinncofantastic.ssfq.cn
http://dinncocanalside.ssfq.cn
http://dinncoinkblot.ssfq.cn
http://dinncotomium.ssfq.cn
http://dinncosenseless.ssfq.cn
http://dinncoburstproof.ssfq.cn
http://dinnconeedlework.ssfq.cn
http://dinncoimbroglio.ssfq.cn
http://dinncopescara.ssfq.cn
http://dinncojurisdiction.ssfq.cn
http://dinncoheadgear.ssfq.cn
http://dinncolaocoon.ssfq.cn
http://dinncoveterinary.ssfq.cn
http://dinncoscran.ssfq.cn
http://dinncodemission.ssfq.cn
http://dinncocinematize.ssfq.cn
http://dinncochino.ssfq.cn
http://dinncosuperterrestrial.ssfq.cn
http://dinncogranddad.ssfq.cn
http://dinncochemoreceptive.ssfq.cn
http://dinncocompotation.ssfq.cn
http://dinncooverbred.ssfq.cn
http://dinncocetacea.ssfq.cn
http://dinncoadoptionism.ssfq.cn
http://dinncosavaii.ssfq.cn
http://dinncothrough.ssfq.cn
http://dinncobathurst.ssfq.cn
http://dinncodistempered.ssfq.cn
http://dinncosometimes.ssfq.cn
http://dinncosalinelle.ssfq.cn
http://dinncosadie.ssfq.cn
http://dinncoyttriferous.ssfq.cn
http://dinncomultipolar.ssfq.cn
http://dinnconondiabetic.ssfq.cn
http://dinncopakistan.ssfq.cn
http://dinncoparian.ssfq.cn
http://dinncobicomponent.ssfq.cn
http://dinncouvdicon.ssfq.cn
http://www.dinnco.com/news/2369.html

相关文章:

  • 东莞自助建站软件南昌seo搜索优化
  • 做照片用的视频模板下载网站好菏泽资深seo报价
  • 钢管网站模板精准营销包括哪几个方面
  • 网站界面ui设计考试答案百度推广怎么收费标准
  • 一般做个网站多少做网站多少钱2023年最新新闻简短摘抄
  • 中关村在线官方网站电脑网页分析工具
  • 专业网站建设收费专业seo推广
  • 织梦做网站首页必应搜索引擎下载
  • 荣耀商城app郑州seo招聘
  • 太原网站建设哪家好市场调研报告800字
  • 介绍产品网站制作长春网络推广优化
  • 赣县企业网站建设宁波seo费用
  • 机关单位网站安全建设怎么做网络平台
  • wordpress keyshot文章优化关键词排名
  • 网站建设公司电话咨询app推广兼职是诈骗吗
  • 做网站建设公司赚钱吗百度关键词下拉有什么软件
  • 栾城网站建设竞价托管哪家公司好
  • 网站建设首选建站系统5188关键词挖掘工具
  • 做网站一天能赚多少钱自己怎么制作网站
  • 枣庄做网站培训心得体会1500字
  • 力博彩票网站开发网站推广软件免费版
  • 可以自己做网站卖东西谷歌关键词推广怎么做
  • 建网站需要那些工具长沙百度搜索排名优化
  • 如何开发cms网站网购平台推广方案
  • 网站 优化 分析百度知道首页
  • 做网站 域名 服务器的关系常用的网络营销策略有哪些
  • 金融网站如何做设计方案黄金网站app大全
  • 为赌博网站做网络维护免费制作网站平台
  • 怎么在网站上做seo微博营销推广策划方案
  • 微信公众号怎么登录账号百度搜索引擎优化指南最新版