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. 轻量级监控平台之cpu监控

    轻量级监控平台之cpu监控脚本 #!/bin/bash #进程监控脚本 #功能需求: 上报机器的硬件层面-cpu负载数据 . /etc/profile . ~/.bash_profile pushur ...

  2. div里面的元素在【垂直 方向】上水平分布 使用calc()函数动态计算

    1==>如何让div里面的元素在[垂直 方向]上水平分布.important-dec{ height: 121px; //必须固定高度 flex-direction: column; //垂直排 ...

  3. 关闭Chrome浏览器的广告

    生活没有绝对的对与错:代码就不一样了,错了就编译不过,也正是因为这样,编程的人思维有时也会陷入一种狭隘中,这就是把工作和生活没有分开.Win10 右下角的广告就像程序调试中的"警告" ...

  4. HTML连载42-清空默认边距、文字行高

    一.            webstorm取色技巧:webstorm内置了颜色取色器,我们对某种颜色未知的时候,可以利用下图中的取色器,进行颜色识别. 二.系统会默认给body添加外边距,因此我们对 ...

  5. 大话设计模式Python实现-桥接模式

    桥接模式(Bridge Pattern):将抽象部分与它的实现部分分离,使它们都可以独立地变化. 下面是一个桥接模式的demo: #!/usr/bin/env python # -*- coding: ...

  6. PHP 高级面试题 - 如果没有 mb 系列函数,如何切割多字节字符串

    需求 如果需要将可能含有中文的字符串进行拆分成数组,我们下面以 utf-8 编码为例. 解决方案一 我习惯的方法可能是: mb_internal_encoding("UTF-8") ...

  7. XML与DTD(够用)

    1: 概述 1.1 什么是XML 1.2 三个重点 1.3规则 1.4 常用转义 2: Xml声明 XML 中,空格会被保留 XML 以 LF 存储换行 3:Xml标签 4:Xml元素 5:XML 属 ...

  8. Java设计模式:Flyweight(享元)模式

    概念定义 享元(Flyweight)模式运用共享技术高效地支持大量细粒度对象的复用. 当系统中存在大量相似或相同的对象时,有可能会造成内存溢出等问题.享元模式尝试重用现有的同类对象,如果未找到匹配的对 ...

  9. sql server 列字段拼接 —— STUFF

    原始数据: sql语句 SELECT DISTINCT l.family_id, )) ,,'' ) isc_id FROM dbo.Addresses l 结果数据:

  10. python连接Oracle工具类

    上代码: # -*- coding:utf-8 -*- import cx_Oracle import pandas as pd class ORACLE(object): def __init__( ...