Skip to content

Commit 8e4aac0

Browse files
committed
0626
1 parent d16e59a commit 8e4aac0

30 files changed

+867
-0
lines changed

sort_by_leetcode/bit manipulation/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
3+
Given an array of integers, every element appears twice except for one. Find that single one.
4+
5+
Note:
6+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
7+
8+
"""
9+
10+
"""
11+
利用异或,两个相同的数的异或为0
12+
"""
13+
14+
15+
class Solution(object):
16+
def singleNumber(self, nums):
17+
"""
18+
:type nums: List[int]
19+
:rtype: int
20+
"""
21+
res = 0
22+
for i in nums:
23+
res = res ^ i
24+
return res
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
3+
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
4+
5+
You may assume that the array is non-empty and the majority element always exist in the array.
6+
7+
Credits:
8+
Special thanks to @ts for adding this problem and creating all test cases.
9+
10+
Subscribe to see which companies asked this question.
11+
12+
"""
13+
14+
"""思路,因为出现次数最多的元素大于元素个数的一半,所以使用count和cand,设目标值为a
15+
"""
16+
17+
18+
class Solution(object):
19+
def majorityElement(self, nums):
20+
"""
21+
:type nums: List[int]
22+
:rtype: int
23+
"""
24+
cand = nums[0]
25+
count = 1
26+
for i in nums[1:]:
27+
if count == 0:
28+
cand, count = i, 1
29+
else:
30+
if i == cand:
31+
count = count + 1
32+
else:
33+
count = count - 1
34+
return cand
35+
36+
"""一行的思路"""
37+
class Solution(object):
38+
def majorityElement(self, nums):
39+
"""
40+
:type nums: List[int]
41+
:rtype: int
42+
"""
43+
return sorted(nums)[len(nums)/2]
44+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
3+
Reverse bits of a given 32 bits unsigned integer.
4+
5+
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
6+
7+
Follow up:
8+
If this function is called many times, how would you optimize it?
9+
10+
Related problem: Reverse Integer
11+
12+
Credits:
13+
Special thanks to @ts for adding this problem and creating all test cases.
14+
15+
"""
16+
17+
"""
18+
19+
利用十进制转二进制的方法,考虑十进制13
20+
13 / 2 = 6 ----1
21+
6 /2 = 3 ----0
22+
3 /2 = 1 ----1
23+
1 /2 = 0 ----1
24+
二进制是1101,前面补全0,而反转的二进制是1011,后面补全0
25+
所以我们只需要按照除的顺序依次进入数组,后面不组32位用0补齐即可。
26+
27+
"""
28+
29+
30+
class Solution:
31+
# @param n, an integer
32+
# @return an integer
33+
def reverseBits(self, n):
34+
stack = []
35+
while n:
36+
stack.append(n % 2)
37+
n = n / 2
38+
while len(stack) < 32:
39+
stack.append(0)
40+
ret = 0
41+
for num in stack:
42+
ret = ret * 2 + num
43+
return ret
44+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
3+
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
4+
5+
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.
6+
7+
Credits:
8+
Special thanks to @ts for adding this problem and creating all test cases.
9+
10+
11+
12+
"""
13+
"""
14+
这里用到的二进制知识是:一个数与比自己小1的数按位与运算,会将自己所对应的二进制数的最后一个1变为0。
15+
考虑如下的例子: 13的二进制数为1101,12的二进制数为1100
16+
则 (1101)& (1100) --》(1100)
17+
1100代表的数是10,9的二进制数为1001,则:
18+
1100 & 1001 --》1000
19+
依次类推
20+
"""
21+
22+
class Solution(object):
23+
def hammingWeight(self, n):
24+
"""
25+
:type n: int
26+
:rtype: int
27+
"""
28+
count = 0
29+
while n:
30+
n = n & (n-1)
31+
count = count + 1
32+
return count
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
3+
Given an integer, write a function to determine if it is a power of two.
4+
5+
Credits:
6+
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
7+
8+
"""
9+
"""跟191题一样的思路,2的倍数的对应二进制数只有1位是1"""
10+
11+
class Solution(object):
12+
def isPowerOfTwo(self, n):
13+
"""
14+
:type n: int
15+
:rtype: bool
16+
"""
17+
return n>0 and not (n & n-1)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
3+
4+
For example,
5+
Given nums = [0, 1, 3] return 2.
6+
7+
Note:
8+
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
9+
10+
"""
11+
"""利用求和公式求解"""
12+
class Solution(object):
13+
def missingNumber(self, nums):
14+
"""
15+
:type nums: List[int]
16+
:rtype: int
17+
"""
18+
n = len(nums)
19+
return n * (n+1) / 2 - sum(nums)
20+
21+
22+
"""二进制的思路
23+
仍然利用两个相同的数的异或为0
24+
"""
25+
26+
class Solution(object):
27+
def missingNumber(self, nums):
28+
"""
29+
:type nums: List[int]
30+
:rtype: int
31+
"""
32+
result = len(nums)
33+
for index,value in enumerate(nums):
34+
result ^= index
35+
result ^= value
36+
return result
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
3+
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
4+
5+
Example:
6+
Given num = 16, return true. Given num = 5, return false.
7+
8+
Follow up: Could you solve it without loops/recursion?
9+
10+
Credits:
11+
Special thanks to @yukuairoy for adding this problem and creating all test cases.
12+
13+
"""
14+
"""4的倍数首先要满足2的倍数的条件,然后他减1要是3的倍数"""
15+
16+
class Solution(object):
17+
def isPowerOfFour(self, num):
18+
"""
19+
:type num: int
20+
:rtype: bool
21+
"""
22+
return num>0 and (num & num-1)==0 and (num-1)%3==0
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
3+
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
4+
5+
Example:
6+
Given a = 1 and b = 2, return 3.
7+
8+
Credits:
9+
Special thanks to @fujiaozhu for adding this problem and creating all test cases.
10+
11+
"""
12+
13+
"""
14+
思路:强大的位运算,不能用+ and -,考虑位运算,按位加
15+
如果两位都是0,则取0,如果两位中有一位是1,则是1,如果两位都是1,则是10,那么我们可以用异或来表示这种关系,但是两个都是1的情况下,还需要在前一位补一个1,所以再用按位与并移1位来表示需要补上的一个1
16+
例如考虑四位二进制位的3(0011)和 5(0101)相加:
17+
第一轮
18+
a = 0011 ^ 0101 = 0110
19+
b = 0011 & 0101 = 0001 << 1 = 0010
20+
第二轮:
21+
a = 0110 ^ 0010 = 0100
22+
b = 0110 & 0010 = 0010 << 1 = 0100
23+
第三轮:
24+
a = 0100 ^ 0100 = 0000
25+
b = 0100 & 0100 = 0100 << 1 = 1000
26+
最后一轮:
27+
a = 0000 ^ 1000 = 1000
28+
b = 0000 & 1000 = 0000迭代终止,返回a
29+
30+
"""
31+
"""但是,这个对python不适用!,当输入负数时,会超时"""
32+
class Solution(object):
33+
def getSum(self, a, b):
34+
"""
35+
:type a: int
36+
:type b: int
37+
:rtype: int
38+
"""
39+
if a==0:
40+
return b
41+
if b==0:
42+
return a
43+
while b:
44+
carry = a & b
45+
a = a ^ b
46+
b = carry << 1
47+
return a
48+
49+
50+
"""python solution"""
51+
class Solution(object):
52+
def getSum(self, a, b):
53+
"""
54+
:type a: int
55+
:type b: int
56+
:rtype: int
57+
"""
58+
# 32 bits integer max
59+
MAX = 0x7FFFFFFF
60+
# 32 bits interger min
61+
MIN = 0x80000000
62+
# mask to get last 32 bits
63+
mask = 0xFFFFFFFF
64+
while b != 0:
65+
# ^ get different bits and & gets double 1s, << moves carry
66+
a, b = (a ^ b) & mask, ((a & b) << 1) & mask
67+
# if a is negative, get a's 32 bits complement positive first
68+
# then get 32-bit positive's Python complement negative
69+
"""有疑问,python把有符号数当作无符号数处理"""
70+
return a if a <= MAX else ~(a ^ mask)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
3+
Given two strings s and t which consist of only lowercase letters.
4+
5+
String t is generated by random shuffling string s and then add one more letter at a random position.
6+
7+
Find the letter that was added in t.
8+
9+
Example:
10+
11+
Input:
12+
s = "abcd"
13+
t = "abcde"
14+
15+
Output:
16+
e
17+
18+
Explanation:
19+
'e' is the letter that was added.
20+
21+
"""
22+
"""可以用hash table,当然更简单的还是利用两个相同的数异或值为0"""
23+
class Solution(object):
24+
def findTheDifference(self, s, t):
25+
"""
26+
:type s: str
27+
:type t: str
28+
:rtype: str
29+
"""
30+
res = 0
31+
for i in s+t:
32+
res ^= ord(i)
33+
return chr(res)

0 commit comments

Comments
 (0)