DAY 26
0

# Day 26: LeetCode Hard+Medium

## Source

https://leetcode.com/problems/word-search-ii/

## 作法:

`clone` Word Search的寫法，再稍作更動，並加速一下。

@JohnTing

## Source

https://leetcode.com/problems/bitwise-and-of-numbers-range/

### Discuss[1]

#### Observe

Ex: [5,8]

``````    101 <- len = 3
110
&   111
1000 <- len = 4
-------
0000

``````
``````class Solution:
def rangeBitwiseAnd(self, left: int, right: int) -> int:

if len(bin(left)) != len(bin(right)):
return 0
for i in range(left+1,right+1):
left = left & i
return left
``````

### YT[2]

Bitwise AND of Numbers Range

#### Observe

Ex: [5,7]

``````idx 210
-------
101
110
&   111
-------
100
-
``````
##### 作法

index 2左邊(包含2)`保留`，index 2右邊`捨棄`

PS: 右移 (>>1)

PS: m/n == m或n

##### 程式碼
``````class Solution:
def rangeBitwiseAnd(self, left: int, right: int) -> int:
'''
>> -> /2
<< -> *2
421
------
5: 101
6: 110
7: 111
-------
100
OXX  O: the same X: diffirent
可移掉右邊清為空留下左邊
移掉右邊 -> 最後成00
留下左邊 -> 不動

op1: >> also means drop right bit
do (>> 1) + counter until m == n

op2: << counter
'''

cnt = 0
while left!=right:
left >>= 1
right >>= 1
cnt+=1
return right << cnt

``````

FRIENDS30