-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sorting, Binary Search, Bit Operation
- Loading branch information
1 parent
ce1feac
commit 6416476
Showing
9 changed files
with
175 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Solution: | ||
def singleNumber(self, nums: List[int]) -> int: | ||
result = 0 | ||
for num in nums: | ||
result ^= num | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution: | ||
def hammingWeight(self, n: int) -> int: | ||
bits = 0 | ||
mask = 1 | ||
for i in range(32): | ||
if (n & mask) != 0: | ||
bits += 1 | ||
mask <<= 1 | ||
|
||
return bits | ||
|
||
|
||
class Solution2: | ||
def hammingWeight(self, n: int) -> int: | ||
count = 0 | ||
|
||
while n: | ||
n &= n - 1 | ||
count += 1 | ||
|
||
return count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Solution: | ||
def search(self, nums: List[int], target: int) -> int: | ||
if not nums: | ||
return -1 | ||
|
||
left, right = 0, len(nums) - 1 | ||
while left < right: | ||
|
||
mid = (left + right) // 2 | ||
|
||
if nums[mid] > nums[right]: | ||
left = mid + 1 | ||
else: | ||
right = mid | ||
|
||
pivot = left | ||
|
||
left, right = 0, len(nums) - 1 | ||
while left <= right: | ||
mid = (left + right) // 2 | ||
mid_pivot = (mid + pivot) % len(nums) | ||
|
||
if nums[mid_pivot] > target: | ||
right = mid - 1 | ||
elif nums[mid_pivot] < target: | ||
left = mid + 1 | ||
else: | ||
return mid_pivot | ||
|
||
return -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Solution: | ||
def getSum(self, a: int, b: int) -> int: | ||
MASK = 0xFFFFFFFF | ||
INT_MAX = 0x7FFFFFFF | ||
|
||
while b != 0: | ||
a, b = (a ^ b) & MASK, ((a & b) << 1) & MASK | ||
|
||
if a > INT_MAX: | ||
a = ~(a ^ MASK) | ||
return a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class Solution: | ||
def validUtf8(self, data: List[int]) -> bool: | ||
def check(size): | ||
for i in range(start+1, start + size + 1): | ||
if i >= len(data) or (data[i] >> 6) != 0b10: | ||
return False | ||
return True | ||
|
||
start = 0 | ||
while start < len(data): | ||
|
||
first = data[start] | ||
|
||
if (first) >> 3 == 0b11110 and check(3): | ||
start += 4 | ||
elif (first) >> 4 == 0b1110 and check(2): | ||
start += 3 | ||
elif (first) >> 5 == 0b110 and check(1): | ||
start += 2 | ||
elif (first) >> 7 == 0: | ||
start += 1 | ||
else: | ||
return False | ||
|
||
return True | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Solution: | ||
def hammingDistance(self, x: int, y: int) -> int: | ||
return bin(x^y).count('1') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import re | ||
import collections | ||
import itertools | ||
import heapq | ||
import bisect | ||
|
||
|
||
class Solution: | ||
def search(self, nums: List[int], target: int) -> int: | ||
|
||
left, right = 0, len(nums) - 1 | ||
|
||
while left <= right: | ||
mid = (left + right) // 2 | ||
|
||
if nums[mid] < target: | ||
left = mid + 1 | ||
elif nums[mid] > target: | ||
right = mid - 1 | ||
else: | ||
return mid | ||
|
||
return -1 | ||
|
||
|
||
class Solution2: | ||
def search(self, nums: List[int], target: int) -> int: | ||
|
||
def binary_search(left, right): | ||
if left <= right: | ||
|
||
mid = (left + right) // 2 | ||
|
||
if nums[mid] < target: | ||
return binary_search(mid+1, right) | ||
elif nums[mid] > target: | ||
return binary_search(left, mid-1) | ||
else: | ||
return mid | ||
else: | ||
return -1 | ||
|
||
return binary_search(0, len(nums) - 1) | ||
|
||
|
||
class Solution3: | ||
def search(self, nums: List[int], target: int) -> int: | ||
|
||
index = bisect.bisect_left(nums, target) | ||
|
||
if index < len(nums) and nums[index] == target: | ||
return index | ||
else: | ||
return -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution: | ||
def sortColors(self, nums: List[int]) -> None: | ||
i,j,k = 0, 0, len(nums) | ||
|
||
while j < k: | ||
if nums[j] < 1: | ||
nums[i], nums[j] = nums[j], nums[i] | ||
i += 1 | ||
j += 1 | ||
elif nums[j] > 1: | ||
k -= 1 | ||
nums[j], nums[k] = nums[k], nums[j] | ||
else: | ||
j += 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters