Skip to content

Commit c53be1b

Browse files
Chris WuChris Wu
authored andcommitted
add solutions
1 parent 3df6acb commit c53be1b

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
First, we search the target by switch pointer `p`.
3+
* Starting at middle. `p = (l+r)/2`.
4+
* If the `nums[p]>target`, we do the same search on the left-half of nums.
5+
* If the `nums[p]<target`, we do the same search on the right-half of nums.
6+
* Until we find the target.
7+
If the target is not in the `nums` the pointer `p` will stop moving. `return [-1, -1]`.
8+
9+
Second, we move pointers, `l`, `r` to find the right-most and left-most targets.
10+
11+
The time complexity is O(LogN), becuase we use the concept of binary search to find the `target`.
12+
"""
13+
class Solution(object):
14+
def searchRange(self, nums, target):
15+
if nums is None or len(nums)==0:
16+
return [-1, -1]
17+
18+
l = 0
19+
r = len(nums)
20+
p = (l+r)/2
21+
22+
while nums[p]!=target:
23+
if nums[p]>target: r = p
24+
else: l = p
25+
26+
p_next = (l+r)/2
27+
if p==p_next: return [-1, -1]
28+
p = p_next
29+
30+
l = r = p
31+
while r+1<len(nums) and nums[r+1]==target:
32+
r = r+1
33+
while 0<=l-1 and nums[l-1]==target:
34+
l = l-1
35+
return [l, r]

problems/hamming-distance.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
The `^` (XOR) operator returns `1` if the bits are different.
3+
So the main idea is to count the `1` in the `x^y`.
4+
The "easy" way is
5+
```
6+
class Solution(object):
7+
def hammingDistance(self, x, y):
8+
return bin(x^y).count('1')
9+
```
10+
This solution takes `O(N)`, `N` is the length of the `bin(x^y)`.
11+
12+
Or we can use some tricks, lets say `A` is `x^y` in binary form.
13+
`A&(A-1)` turns all the bits *right the right-most 1* to 0, including the right-most 1.
14+
```
15+
A = 10110
16+
A = A&(A-1) #10100
17+
A = A&(A-1) #10000
18+
A = A&(A-1) #00000
19+
```
20+
In other words, `A = A&(A-1)` will eliminate a 1 one at a time, starting from right.
21+
Until `A` is all 0.
22+
So we can use this trick to count the `1`.
23+
The run time will be `O(K)`, `K` is the number of 1 in `A`.
24+
"""
25+
class Solution(object):
26+
def hammingDistance(self, x, y):
27+
xor = x^y
28+
count = 0
29+
while xor:
30+
count += 1
31+
xor = xor&(xor-1)
32+
return count

problems/number-complement.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
First, we convert the num to its birary.
3+
```
4+
>>> bin(5)
5+
>>> '0b101'
6+
```
7+
8+
Second, we need to return the base10 of binary's the complement.
9+
Complement is easy `'101' => '010'`.
10+
Turn to base10:
11+
```
12+
'010' => 0*pow(2, 2) + 1*pow(2, 1) + 0*pow(2, 0)
13+
'11011' => 1*pow(2, 4) + 1*pow(2, 3) + 0*pow(2, 2) + 1*pow(2, 1) + 1*pow(2, 0)
14+
```
15+
16+
Basics bit manipulation.
17+
<https://www.youtube.com/watch?v=NLKQEOgBAnw>
18+
"""
19+
class Solution(object):
20+
def findComplement(self, num):
21+
b = bin(num)[2:]
22+
opt = 0
23+
for i, c in enumerate(reversed(b)):
24+
if c=='0': opt+=pow(2, i)
25+
return opt

0 commit comments

Comments
 (0)