338. Counting Bits(计算小于n的各个数值对应的二进制1的个数)

思路:通过奇偶判断,if i是偶数,a[i]=a[i/2],if i是奇数,a[i]=a[i-1]+1。

class Solution {
public:
vector<int> countBits(int num) {
vector<int>res(num+, ); for(int i=;i<=num;i++)
{
if(i&)
res[i] = res[i-]+;//如果尾数为1,那么结果就是奇数,等于前一个值加1
else
res[i] = res[i/];//如果尾数为0,那么就是偶数,等于他一半的值
}
return res;
}
};

136. Single Number(只有一个元素出现1次,其他出现2次,寻找只出现一次的数值)

思路:异或 解决,异或性质: x^x=0,x^0=x

class Solution {
public:
int singleNumber(vector<int>& nums) {
int n=nums.size();
int res=nums[];
for (int i=;i<n;i++){
res=res^nums[i];
}
return res; }
};

137. Single Number II (只有一个元素出现1次,其他出现3次,寻找只出现一次的数值);

思路:按照位运算,每个位置是上1的个数为3,则变0

 class Solution {
public:
int singleNumber(vector<int>& nums) {
int n = nums.size(),answer=;
for (int j = ; j < ; j++)
{
int a = <<j,count=;//count 记录每个位置上1的个数
for (int i = ; i < n; i++)
{
if (a&nums[i]) count++;//统计转化为二进制之后,在a的位置上1的个数
}
if (count % != )//如果每个位置上的1不为3,则最后的数字中,这个位置中一定是1
answer |= a;
}
return answer;
}
};

260. Single Number III(有两个元素出现1次,其他出现2次,寻找两个出现一次的数值);

思路:整体异或结果,而后在这个结果中某个位置为1的位置,进而分割成两个部分,分别异或,者可得到结果

class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int n = nums.size();
vector<int> vec;
int res=;
for (int i = ; i < n; i++)
{
res ^= nums[i];
}
int index = ;//记录异或的结果第一个为1的位置,0表示第一个位置为1
for (int i = ; i < ; i++)
{
if (res & == )
break;
index++;
res = res >> ;
}
int num1=, num2 = ;
for (int i = ; i < n; i++)
{
if ((nums[i] >> index) & == )//以index位置将述组分为两段异或
num1 ^= nums[i];
else
num2 ^= nums[i];
}
vec.push_back(num1);
vec.push_back(num2);
return vec;
}
};

LeetCode 二进制问题的更多相关文章

  1. leetcode -- 二进制

    leetcode -- 二进制 在学习编程语言的运算符时,大部分语言都会有与,或等二进制运算符,我在初期学习这些运算符的时候,并没有重点留意这些运算符,并且在后续的业务代码中也没有频繁的使用过,直到后 ...

  2. leetcode 二进制求和 python

    class Solution: def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str ...

  3. [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  4. [LeetCode] Special Binary String 特殊的二进制字符串

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  5. [LeetCode] Count Binary Substrings 统计二进制子字符串

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  6. [LeetCode] Binary Gap 二进制间隙

    Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...

  7. 【leetcode 简单】 第九十三题 二进制手表

    二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表读取 “3:25”. ...

  8. LeetCode:二进制手表【401】

    LeetCode:二进制手表[401] 题目描述 二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右 ...

  9. LeetCode:二进制求和【67】

    LeetCode:二进制求和[67] 题目描述 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11" ...

随机推荐

  1. 04. Go 语言流程控制

    Go 语言流程控制 流程控制是每种编程语言控制逻辑走向和执行次序的重要部分,流程控制可以说是一门语言的"经脉". Go 语言的常用流程控制有 if 和 for,而 switch 和 ...

  2. 央行辟谣未发行“DC/EP”和“DCEP” 法定数字货币仍在测试阶段

    http://www.sohu.com/a/354709423_100157595 近期,中国央行再度就法定数字货币发布公告,指出目前系统仍处于研究测试过程中,市场上交易“DC/EP”或“DCEP”均 ...

  3. DirectShow 进行视频预览和录制

    这一篇讲怎么采集摄像头图像并预览,以及录制视频到本地. 程序实现流程 这里通过使用 CaptureGraphBuilder 来简化 Graph 的创建流程. 具体流程如下: 初始化 COM 库 创建各 ...

  4. python Qt5 实战(一)按钮颜色

    工作中,工具用到了python Qt5,涉及到了按钮颜色,这里就做个总结.也顺便给要用这块的同仁抛出来一个砖头,把大牛引出来做个指导. 一般设置按钮的颜色有三种表达:如下所示:具体的怎么使用,估计要看 ...

  5. centos6和centos7的防火墙基本命令

    一.centos6: 1.firewall的基本启动/停止/重启命令 $查看防火墙状态: service iptables status (/etc/init.d/iptables status) $ ...

  6. spring cloud 2.x版本 Gateway自定义过滤器教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  7. 正则表达式中的.*?和python中re.S参数的详解

    本章的内容主要是为讲解在正则表达式中常用的.*?和re.S! 在正则表达式中有贪婪匹配和最小匹配:如下为贪婪匹配(.*) import re match = re.search(r'PY.*', 'P ...

  8. [05]ASP.NET Core 中的 Main 方法

    ASP.NET Core 中的 Main 方法 本文作者:梁桐铭- 微软最有价值专家(Microsoft MVP) 文章会随着版本进行更新,关注我获取最新版本 本文出自<从零开始学 ASP.NE ...

  9. Redisson实现分布式锁(2)—RedissonLock

    Redisson实现分布式锁(2)-RedissonLock 有关Redisson实现分布式锁上一篇博客讲了分布式的锁原理:Redisson实现分布式锁---原理 这篇主要讲RedissonLock和 ...

  10. MVC过滤器:自定义异常过滤器使用案例

    在上一篇文章中讲解了自定义异常过滤器,这篇文章会结合工作中的真实案例讲解一下如何使用自定义异常过滤器. 一.需求 本案例要实现的功能需求:在发生异常时记录日志,日志内容包括发生异常的Controlle ...