LeetCode之Bit Manipulation题目汇总

算法技巧-位运算(Bit Manipulation) 算法技巧-位运算(Bit Manipulation)一 . 位运算简介二 . 位运算规则A . AND与运算B . OR或运算C . XOR异或运算D . NOT取反运算E . 位移运算三 . 位运算技巧A . AND与运算技巧1 . 清零B . OR或运算技巧C . XOR异或运算技巧D . NOT取反运算技巧E . 位移运算技巧F . 综合运算技巧四 . 位运算例题五 . 位运算资料A . 关... 阅读详情

Bitwise AND of Numbers Range

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.

For example, given the range [5, 7], you should return 4.

Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.


实质:求m和n二进制编码中,相同的前缀

    public int rangeBitwiseAnd(int m, int n) {

        int bit = 0;

        while (m != n) {
            m >>= 1;
            n >>= 1;
            bit++;
        }

        return m << bit;
    }

参考:LeetCode 169 Majority Element

A Linear Time Majority Vote Algorithm 一个典型的算法,可以在一次遍历,时间复杂度是O(1),空间复杂度是O(1)的情况下完成。

    public int majorityElement(int[] nums) {

        int m = nums[0];
        int c = 1;

        for (int i = 1; i < nums.length; i++) {
            if (m == nums[i]) {
                c++;
            } else if (c > 1) {
                c--;
            } else {
                m = nums[i];
            }
        }

        return m;
    }

Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.

Hint:

  1. How many majority elements could it possibly have?
  2. Do you have a better hint? Suggest it!

参考:LeetCode 229 Majority Element II

因为题目中说是大于⌊ n/3 ⌋次数的元素,这样的元素最多只能有2个。

    public List<Integer> majorityElement(int[] nums) {

        List<Integer> rt = new ArrayList<Integer>();

        if (nums == null || nums.length == 0) {
            return rt;
        }

        int m1 = nums[0];
        int m2 = 0;

        int c1 = 1;
        int c2 = 0;

        for (int i = 1; i < nums.length; i++) {
            int x = nums[i];
            if (x == m1) {
                c1++;
            } else if (x == m2) {
                c2++;
            } else if (c1 == 0) {
                m1 = x;
                c1 = 1;
            } else if (c2 == 0) {
                m2 = x;
                c2 = 1;
            } else {
                c1--;
                c2--;
            }
        }

        c1 = 0;
        c2 = 0;

        for (int i = 0; i < nums.length; i++) {
            if (m1 == nums[i]) {
                c1++;
            } else if (m2 == nums[i]) {
                c2++;
            }
        }

        if (c1 > nums.length / 3) {
            rt.add(m1);
        }

        if (c2 > nums.length / 3) {
            rt.add(m2);
        }

        return rt;
    }

Missing Number

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

For example, 
Given nums = [0, 1, 3] return 2.

Note: 
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

Credits: 
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.


参考:LeetCode 268 Missing Number

既然是0~n只差其中一个数,不如再次加入0~n的序列,这样就和LeetCode 136 Single Number一样了,missing number只出现一次,其余都是出现两次。

    public int missingNumber(int[] nums) {

        int rt = nums.length;

        for (int i = 0; i < nums.length; i++) {
            rt = rt ^ nums[i] ^ i;
        }

        return rt;
    }

Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight).

For example, the 32-bit integer ’11’ has binary representation 00000000000000000000000000001011, so the function should return 3.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.


1. JDK:int bitCount(int i)

效率肯定高,JDK源码+_+

    /**
     * Returns the number of one-bits in the two's complement binary
     * representation of the specified {@code int} value.  This function is
     * sometimes referred to as the <i>population count</i>.
     *
     * @param i the value whose bits are to be counted
     * @return the number of one-bits in the two's complement binary
     *     representation of the specified {@code int} value.
     * @since 1.5
     */
    public static int bitCount(int i) {
        // HD, Figure 5-2
        i = i - ((i >>> 1) & 0x55555555);
        i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
        i = (i + (i >>> 4)) & 0x0f0f0f0f;
        i = i + (i >>> 8);
        i = i + (i >>> 16);
        return i & 0x3f;
    }

2. 思路

思路一☹:分别判断每一位,有n位就要判断n次。

思路二☺:有几个1就判断几次。参考如何判断一个数是2的n次方?判断i&(i-1)是不是0即可~

    public int hammingWeight(int n) {

        int rt = 0;

        while (n != 0) {
            n = n & (n - 1);
            rt++;
        }

        return rt;
    }

Power of Two

Given an integer, write a function to determine if it is a power of two.

Credits: 
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.


    public boolean isPowerOfTwo(int n) {

        if (n <= 0) {
            return false;
        }
        return (n & (n - 1)) == 0;
    }

Repeated DNA Sequences

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTCCG”. When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.

Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.

For example,

Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT",

Return:
["AAAAACCCCC", "CCCCCAAAAA"].

考察位图。按位操作,A C G T分别用如下bits表示:

A   00
C   01
G   10
T   11

所以10个连续的字符,只需要20位即可表示,而一个int(32位)就可以表示。定义变量hash,后20位表示字符串序列,其余位数置0 。

定义一个set用来存放已经出现过的hash,计算新hash时,如果已经出现过,就放入结果的set中。

    public List<String> findRepeatedDnaSequences(String s) {

        if (s == null || s.length() < 11) {
            return new ArrayList<String>();
        }

        int hash = 0;

        Set<Integer> appear = new HashSet<Integer>();
        Set<String> set = new HashSet<String>();

        Map<Character, Integer> map = new HashMap<Character, Integer>();
        map.put('A', 0);
        map.put('C', 1);
        map.put('G', 2);
        map.put('T', 3);

        for (int i = 0; i < s.length(); i++) {

            char c = s.charAt(i);

            hash = (hash << 2) + map.get(c);
            hash &= (1 << 20) - 1;

            if (i >= 9) {
                if (appear.contains(hash)) {
                    set.add(s.substring(i - 9, i + 1));
                } else {
                    appear.add(hash);
                }
            }
        }

        return new ArrayList<String>(set);
    }

Reverse Bits

Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as**00111001011110000010100101000000**).

Follow up:
If this function is called many times, how would you optimize it?

Related problem: Reverse Integer

Credits:
Special thanks to @ts for adding this problem and creating all test cases.


JDK :int reverse(int i)

    /**
     * Returns the value obtained by reversing the order of the bits in the
     * two's complement binary representation of the specified {@code int}
     * value.
     *
     * @param i the value to be reversed
     * @return the value obtained by reversing order of the bits in the
     *     specified {@code int} value.
     * @since 1.5
     */
    public static int reverse(int i) {
        // HD, Figure 7-1
        i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
        i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
        i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
        i = (i << 24) | ((i & 0xff00) << 8) |
            ((i >>> 8) & 0xff00) | (i >>> 24);
        return i;
    }

思路

把第i位取出,放到结果的第31-i位上。

    public int reverseBits(int n) {

        int rt = 0;

        for (int i = 0; i < 32; i++) {
            rt |= ((n >> i) & 1) << (31 - i);
        }
        return rt;
    }

Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

Note: 
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?


对于出现2次的数字,用异或。因为“相异为1”,所以一个数字异或本身为0,而0异或0仍为0, 一个数字异或0仍为这个数字。

0 ^ 0 = 0 
n ^ 0 = n 
n ^ n = 0
    public int singleNumber(int[] nums) {

        int n = 0;

        for (int i : nums) {
            n ^= i;
        }

        return n;
    }

Single Number II

Given an array of integers, every element appears three times except for one. Find that single one.

Note: 
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?


对于数组{1,1,1,2}来说,分析每一位:

0 0 0 1  
0 0 0 1  
0 0 0 1  
0 0 1 0  
……………  
0 0 1 3 (位数)

可以分析,分别计算每一位1的出现次数n,如果n%3==1,那么结果中这一位就应该是1。

    public int singleNumber(int[] nums) {

        int rt = 0, bit = 1;

        for (int i = 0; i < 32; i++) {
            int count = 0;

            for (int v : nums) {
                if ((v & bit) != 0) {
                    count++;
                }
            }
            bit <<= 1;
            rt |= (count % 3) << i;
        }

        return rt;
    }

Single Number III

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

Note:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.


参考:LeetCode 136 Single Number 
参考:LeetCode 137 Single Number II

“相异为1”,所以一个数字异或本身为0,而0异或0仍为0, 一个数字异或0仍为这个数字。

0 ^ 0 = 0 
n ^ 0 = n 
n ^ n = 0

  1. 只有两个数a、b分别出现了一次,其余的数都是出现了两次
  2. 把整个数组异或,结果等于a^b,而a与b是不相等的,所以a^b必不为0
  3. 按照a^b结果其中一位,把数组分成两个,一组包含a,一组包含b,即问题变成LeetCode 136 Single Number
    public int[] singleNumber(int[] nums) {

        int[] rt = new int[2];

        int n = 0;

        for (int v : nums) {
            n ^= v;
        }

        int m = n & (~(n - 1));

        for (int v : nums) {
            if ((m & v) == 0) {
                rt[0] ^= v;
            } else {
                rt[1] ^= v;
            }
        }

        return rt;
    }

Subsets II

Given a collection of integers that might contain duplicates, nums, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

For example,  
If nums = [1,2,2], a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

使用二进制,不需要递归。对于二进制中的每一位,0表示不选择,1表示选择。则有2^n种情况。

使用HashSet,能够直接过滤重复情况(先对数组排序,题目中也要求每个list非降序排列)。

    public List<List<Integer>> subsetsWithDup(int[] nums) {

        if (nums == null) {
            return null;
        }

        if (nums.length == 0) {
            return new ArrayList<List<Integer>>();
        }

        Set<List<Integer>> set = new HashSet<List<Integer>>();

        // 题目中要求每个list是非降序,所以要先从小到大排序
        Arrays.sort(nums);

        // 对于n位,有2^n种情况
        for (int i = 0; i < Math.pow(2, nums.length); i++) {

            List<Integer> list = new ArrayList<Integer>();
            int tmp = i;

            // 对于每种情况,分别求得二进制中1的个数
            // 0代表不选择,1代表选择
            for (int j = 0; j < nums.length; j++) {
                int bit = tmp & 1;
                tmp >>= 1;
                if (bit == 1) {
                    list.add(nums[j]);
                }
            }
            set.add(list);
        }

        return new ArrayList<List<Integer>>(set);
    }
c语言或者c++ :Bit manipulations 位操作 Define an unsigned long integer and initialise it to the binary pattern: 0101 1111 0001 1000 1001 0010 1101 0011 Using hexadecimal notation print the hex value on the console. Write a function to print a long integer on the console in hex and binary form. Now perform the following operations on the pattern and print out the results of each operation in hex and binary format. Right shift the binary pattern by 4 bits. Left shift the pattern by 6 bits. AND the value with the long int 0x3 AND the value with the char 0x3 OR the value with the unsigned short 0xf00f Exclusive OR the value with the long int 0xF0F0F0F0 立即下载

相关推荐

Mind+掌控板实战:5分钟搞定Easy IoT的MQTT通讯(附完整代码)

本文详细介绍了如何利用掌控板和Mind+图形化编程软件,快速实现基于Easy IoT平台的MQTT通讯。通过清晰的步骤讲解和完整的代码示例,指导用户完成从云端配置、Wi-Fi连接到MQTT消息发布与订阅的全过程,帮助初学者轻松构建物联网设备间的双向对话系统。

weixin_29300241的博客 459

LeetCode分类刷题(一):位操作(Bit Manipulation)

位运算(Bit Manipulation)一直是程序员面试中的一个必须准备的主题, 不过现在面试中位运算出现的次数并不多,主要原因还是位运算太考察技巧了,很多时候很难在短时间内想出来,所以作为面试的题目显得有点太花时间了。 位运算的主要思想是五种运算:与(&amp;),或(|),异或(^),左移(&lt;&lt;),右移(&gt;&gt;)。 位运算的常用技巧如下: n &amp;(n-1)能...

pushup8的博客 1万+

ADS SerDes仿真实战:深入解析Tx_Diff均衡器(EQ)参数对眼图质量的影响

本文以ADS仿真平台为基础,深入解析了SerDes设计中Tx_Diff均衡器(EQ)参数对信号完整性的核心影响。通过实战演示,详细对比了去加重(De-emphasis)与FIR抽头系数两种调节模式,揭示了它们如何有效对抗信道损耗与码间干扰,从而显著改善眼图的高度、宽度与抖动性能,为高速串行链路设计提供关键调优思路。

weixin_29230901的博客 236

Bit Manipulation —— LeetCode题解Single-number系列

Bit Manipulation需要把每一个变量都想象成一串二进制编码,用and, or, not等逻辑进行运算。在single-number系列的题中,需要把int想象成一串32位的二进制编码。所以所这种题要先转换成二进制的思维。解法的确需要“取巧”,如果没有见过此类题的话还挺难想出来的。 Single-number Given an array of integers, every eleme...

芝麻挞 1056

Glue)《Multi-omics single-cell data integration and regulatory inference with graph-linked embedding》

论文引入GLUE框架,通过知识引导图(guidance graph)显式建模跨层调控交互,桥接特征空间,实现准确、鲁棒、可扩展的整合。近年来,单细胞测序技术(如scATAC-seq用于染色质可及性、snmC-seq用于DNA甲基化、scRNA-seq用于转录组)允许同时探测多个组学层,揭示细胞异质性和调控机制。GLUE优于现有方法:桥接特征空间,避免信息丢失,支持多组学和调控推断。用于整合未配对的单细胞多组学数据(如scRNA-seq、scATAC-seq和snmC-seq),并同时进行调控交互推断。

m0_58293236的博客 1155

【AI实战派】从零部署YOLOv8:香橙派AIpro边缘端目标检测全流程解析

本文详细解析了在香橙派AIpro边缘设备上从零部署YOLOv8目标检测模型的全流程。内容涵盖硬件开箱、环境搭建、模型转换(ONNX到昇腾OM格式)以及编写Python推理脚本,并提供了性能优化与实战技巧,帮助开发者高效实现实时边缘AI应用。

weixin_29183637的博客 334

算法思想之位运算(二)

本篇文章讲解了常见算法中的位运算部分,包括了六大常用操作符和常用模板的介绍,并通过几个题目的练习,使我们进一步加深的对位运算的掌握。

2302_81805546的博客 1897

西门子S7-1200 PLC控制步进电机实战:从I/O分配到梯形图,手把手教你实现正反转与调速

本文详细介绍了如何使用西门子S7-1200 PLC控制步进电机,从硬件接线到梯形图编程,实现正反转与三档调速功能。通过I/O分配、脉冲生成逻辑和方向控制等核心步骤,帮助工程师快速掌握PLC控制步进电机的关键技术,提升工业自动化项目的实施效率。

weixin_28733777的博客 547

Bit Manipulation常用总结

1.判断奇偶 奇偶数的特征:如果一个数是偶数,则对于其二进制来说,最低位肯定是0;如果一个数是奇数,则对于其二进制来说,最低位肯定是1,因为在二进制表示中,只有 会产生1 所以可以利用这一特征来判断奇偶数 if((num&1) == 0) { //偶数 } else { //奇数 } 注意:不用写为 num&1 == 0 ,这样的判断是错误的,必须给前面...

Jared的博客 1603

AgentScope Java 2.0 项目实战: 从零构建自己的个人AI助手“小龙虾“

本文系统介绍如何基于Spring Boot WebFlux、React 18和TypeScript,从零搭建一个本地化的个人AI助手管理平台——AgentScope Paw。项目支持多Agent创建与管理、SSE流式对话、工作区文件CRUD、技能市场、子智能体孵化、AI辅助起草Agent配置等核心功能,并深度集成钉钉、企业微信、飞书、GitHub等IM通道。

program哲学 1460

有效Dk值提取方法的仿真分析

通过本文的仿真对比分析,提取有效Dk值方面,TDR技术在一些固定的场景下,依然有其参考应用价值,而随着VNA功能的不断增强和高精度测量手段的不断更新,双端口Delta-L技术才是主流评估方法,而单端口Delta-L技术则可以作为其降本增效的方案补充。

一只豌豆象的博客 1838

【leetcode】高频题目整理_位操作篇( High Frequency Problems, Bit_Manipulation )

截止至今LeetCode题目总量已经有1582题,估计将来每年平均增长300题左右,大部分人肯定是刷不完的,所以得有选择地刷LeetCode。 一种公认的刷题策略是按类别刷题,可是每个类别也有许多题,在有限的时间里到底该刷哪些题呢?个人根据LeetCode官方给出的每个题目的出现频率,整理并收录了每个类别里高频出现的题目,对于官方统计频率太低的题目,不予收录,最终得到了这个高频题目表格。 例如,对于下图中题号#275与#270的题目将被收录,并且#275出现频率大于#270;而对于题号#1011与#11

嵌入式与Linux那些事的博客 1480

ERROR: Ignored the following yanked versions: 0.8.3, 0.10.19.dev18 的解决办法

关于这个问题,博主是在进行项目中的python依赖包安装的时候遇见的,遇到的异常:关于这个问题,是在。

Spring Boot-Common On With You 647

Leetcode bit manipulation 总结

136. Single Number 利用Xor 求结果 137. Single Number II 统计数字出现的数目,如果是1被整除,这一位是0,如果没有被1整除则这一位是0; 260. Single Number III 利用其中的一位数字区别两个数,然后将两个数构成两个集合,这两个集合分别求XOR,得到的结果就是这两位数。 190. Reverse Bits

Hanwenwangad的专栏 660

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

力扣刷题记录&整理——(十八)Bit Manipulation

整理力扣刷题思路。语言:python题库:来自neetcode:link。

Z_zat的博客 1025

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

LeetCode刷题笔记之Bit Manipulation

常用的bit操作总结 Bit manipulation: 左移(<<):5<<2,转换成2进制,左移2位后,低位补0正数或者负数左移,低位都是用0补 右移( >> ): 5>>3,转换成2进制,右移3位后, 正数右移,高位用0补,负数右移,高位用1补 无符号右移( >>> ): 1.Java中int类型占32位,可以表示一个正数,也可以表示一个负数。正数换算成二进制后的最高位为0,负数的二进制最高为为1, 2.Java的负数二进制表示法:

YB0222的博客 518

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

LeetCode Bit Manipulation 技巧总结

https://leetcode.com/problems/sum-of-two-integers/discuss/84278/A-summary%3A-how-to-use-bit-manipulation-to-solve-problems-easily-and-efficiently Six common bit operator & | ~ ^ >> <<...

Tech in Pieces 539

Leetcode Bit Manipulation 题型总结

Leetcode 136. Single Number https://leetcode.com/problems/single-number/ 137. Single Number II https://leetcode.com/problems/single-number-ii/ 260. Single Number III https://leetcode.com/proble

Hanwenwangad的专栏 431

LeetCode上的Bit Manipulation(位运算)类型的题目

文章目录Easy461. Hamming Distance两个数字的汉明距476. Number Complement 求补码136. Single Number693. Binary Number with Alternating Bits 相互交错的位信息389. Find the Difference Easy 461. Hamming Distance两个数字的汉明距 461. Hamm...

notHeadache的博客 729

LeetCode系列Easy【1】 Algorithms - Bit manipulation

2016.10.09启程。。 LeetCode 答案 Github 来源AlgorithmsBit Manipulation190. Reverse BitsLeetcode 题目链接 Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary

LLuckick的博客 625

[leetcode] Summary of Bit Manipulation

今天刷了几道题,发现leetcode现在都有分类了,所以顺便把以前刷过的bit manipulation相关的题目都看了一遍,下面简单总结一下。 1. Single Number 题目大意:在给定的整数数组中,除了一个数字之外,其他数字都出现了两次,现让求出只出现一次的那个数字。 基本思想:遍历数组一次,将所有数字依次进行异或运算,结果即为所求的数字。因为其他数字都恰好出现了两次,异或得0。

退而结网 747

LeetCode:Heap,Sort,Bit Manipulation,Backtracking

15.Heap先来复习一下stack,queue,heap的特点: 1.stack:FILO(先进后出) 2.queue:FIFO(先进先出) 3.heap:两个rules,1.the tree is complete,2.parent is always smaller than its two children.参考differences1.Design Twitter题意未实现显示前10

1134

LeetCode- Bit Manipulation LeetCode总结(1) —— 位运算

Bit Manipulation(位运算):主要思想参考《剑指Offer》第二章2.4.3节。        一共五种运算:与,或,异或,左移,右移。   常用技巧: (1)n & (n-1)能够消灭n中最右侧的一个1。 (2) 右移:除以2, 左移:乘以2。 (3)异或性质:交换律,0^a=a, a^a=0; (3)将常用字符、数字等均转为按位运算,可以节约空间。 Lee

xuxumoi 4994

Leetcode:bit manipulation

Bitwise AND of Numbers Range My Submissions QuestionGiven a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5

flowbird的博客 482

【LeetCode】位运算 bit manipulation(共32题)

【78】Subsets 给了一个 distinct 的数组,返回它所有的子集。 Example: Input: nums = [1,2,3] Output: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] 题解:我是直接dfs(backtracking)了...

weixin_30498921的博客 148
上一篇: LeetCode之Sort题目汇总
下一篇: LeetCode之Tree题目汇总
_我们的存在
博客等级 码龄13年 203粉丝 246原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值