Skip to content

Commit

Permalink
Sorting, Binary Search, Bit Operation
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqueimaginate committed Mar 5, 2021
1 parent ce1feac commit 6416476
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Coding Test/136.py
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
21 changes: 21 additions & 0 deletions Coding Test/191.py
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
30 changes: 30 additions & 0 deletions Coding Test/33.py
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
11 changes: 11 additions & 0 deletions Coding Test/371.py
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
26 changes: 26 additions & 0 deletions Coding Test/393.py
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

4 changes: 4 additions & 0 deletions Coding Test/461.py
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')

54 changes: 54 additions & 0 deletions Coding Test/704.py
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
15 changes: 15 additions & 0 deletions Coding Test/75.py
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

13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@
## 알고리즘

1. Sorting
> 148, 56, 147, 179, 242, 973
2. Brute Force
3. Binary Search
> 148, 56, 147, 179, 242, 75, 973
2. Binary Search
> 704, 33
3. Bit Operation
> 136, 461, 371, 393, 191
4. Greedy Algorithm
5. Divide & Conquer
6. Bit Operation
7. Dynamic Programming
8. Graph Algorithm
6. Dynamic Programming
7. Graph Algorithm

## 기타 참고

0 comments on commit 6416476

Please sign in to comment.