LeetCode 二进制问题
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 二进制问题的更多相关文章
- leetcode -- 二进制
leetcode -- 二进制 在学习编程语言的运算符时,大部分语言都会有与,或等二进制运算符,我在初期学习这些运算符的时候,并没有重点留意这些运算符,并且在后续的业务代码中也没有频繁的使用过,直到后 ...
- leetcode 二进制求和 python
class Solution: def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str ...
- [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 ...
- [LeetCode] Special Binary String 特殊的二进制字符串
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- [LeetCode] Binary Gap 二进制间隙
Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...
- 【leetcode 简单】 第九十三题 二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表读取 “3:25”. ...
- LeetCode:二进制手表【401】
LeetCode:二进制手表[401] 题目描述 二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右 ...
- LeetCode:二进制求和【67】
LeetCode:二进制求和[67] 题目描述 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11" ...
随机推荐
- 201871010107-公海瑜《面向对象程序设计(java)》第一周学习总结
201871010107-公海瑜<面向对象程序设计(java)>第一周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- 【UOJ276】【清华集训2016】汽水(分数规划+点分治)
点此看题面 大致题意: 给你一棵树,要求你选择一条树上路径,使得这条路径上边权的平均值与定值\(k\)的差的绝对值最小.求出这个最小值. 分数规划 看到平均值,首先就应该想到分数规划吧. 我们二分答案 ...
- Java连载45-继承举例、方法覆盖
一.Java语言中假设一个类没有显式的继承任何类,那么该类默认继承Java SE库中提供的java.lang.Object类 1.快捷键:Ctrl + shift + T:可以在Myeclipse中查 ...
- 《细说PHP》第四版 样章 第23章 自定义PHP接口规范 2
23.1.3 接口的应用和优势 API是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无须访问源码,或理解内部工作机制的细节.接口应用的一些常见场景如下 ...
- 打开Github网站反应慢的问题
解决办法: 为了提高速度,可以使用HOSTS加速对Github网站加载的资源网站域名解析. 具体做法: 修改 C:\Windows\System32\drivers\etc 中的hosts文件(P ...
- python3中的数字类型
今天在学校机房刷python题时发现自己对python中的数字类型不理解,回寝室后百度一下. 现在做一个总结. python中的数字类型有: 整数,布尔值,复数,科学计数法,浮点数 1,整数,大小没有 ...
- eclipse强行停止buliding workspace
使用Eclipse的过程中可能会遇到buliding workspace卡在一半走不动的情况. 出现这个情况往往是因为Eclipse太调皮了,需要拉出去打屁股,打一顿就好了. 开玩笑的,事实上出现这个 ...
- WPF 使用动画设置特殊值的方法
例如设置Visibility属性时: 第一种方式: <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIEleme ...
- HTTP Error 500.35 - ANCM Multiple In-Process Applications in same Process
vs2019 win10 情况:报错 HTTP Error 500.35 - ANCM Multiple In-Process Applications in same Process 微软官方解 ...
- 反射DataTable转实体类
using System; using System.Collections.Generic; using System.Data; using System.Reflection; namespac ...