数据结构与算法中位操作(Bit Manipulation)问题总结归纳。
Single Number
题目描述:LeetCode
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?
分析:最大次数为两次,使用1位即可表示,即采用一个int型变量存储数组的元素和,在累加过程中除以2取余,每一位的变化过程为0->1->0(2/0),利用异或操作实现
Single Number II
题目描述:LeetCode
Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Gray Code
Description: The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the
sequence of gray code. A gray code sequence must begin with 0.
For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
00 - 0
01 - 1
11 - 3
10 - 2
For a given n, a gray code sequence is not uniquely defined.
For example, [0,2,3,1] is also a valid gray code sequence according to the above definition.
|
|