191-Number-Of-1-Bits
https://leetcode.com/problems/number-of-1-bits
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def hammingWeight(self, n: int) -> int:
ans = 0
for i in range(32):
if (n >> i) & 1:
ans += 1
return ans
new Solution().hammingWeight()
Score: 5
Read More
198-House-Robber
https://leetcode.com/problems/house-robber
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def rob(self, nums: List[int]) -> int:
if not nums:
return 0
if len(nums) == 1:
return nums[0]
# dp[i]: = max money of robbing nums[0..i]
dp …
Read More
199-Binary-Tree-Right-Side-View
https://leetcode.com/problems/binary-tree-right-side-view
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
ans = []
q = deque([root])
while q:
size = len(q)
for i in range(size):
root = q.popleft …
Read More
2-Add-Two-Numbers
https://leetcode.com/problems/add-two-numbers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
dummy = ListNode(0)
curr = dummy
carry = 0
while carry or l1 or l2:
if l1:
carry += l1.val
l1 = l1.next …
Read More
20-Valid-Parentheses
https://leetcode.com/problems/valid-parentheses
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isValid(self, s: str) -> bool:
stack = []
for c in s:
if c == '(':
stack.append(')')
elif c == '{':
stack.append('}')
elif c == '[':
stack.append(']')
elif not stack or stack.pop …
Read More
200-Number-Of-Islands
https://leetcode.com/problems/number-of-islands
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
m = len(grid)
n = len(grid[0])
dirs = [0, 1, 0, -1, 0]
def bfs(r, c):
q = deque([(r, c …
Read More
201-Bitwise-And-Of-Numbers-Range
https://leetcode.com/problems/bitwise-and-of-numbers-range
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def rangeBitwiseAnd(self, m: int, n: int) -> int:
return self.rangeBitwiseAnd(m >> 1, n >> 1) << 1 if m < n else m
new Solution().rangeBitwiseAnd()
Score: 5
Read More
202-Happy-Number
https://leetcode.com/problems/happy-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isHappy(self, n: int) -> bool:
def squaredSum(n: int) -> bool:
summ = 0
while n:
summ += pow(n % 10, 2)
n //= 10
return summ
slow = squaredSum(n)
fast = squaredSum …
Read More
203-Remove-Linked-List-Elements
https://leetcode.com/problems/remove-linked-list-elements
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
dummy = ListNode(0, head)
prev = dummy
while head:
if head.val != val:
prev.next = head
prev = prev.next
head = head.next …
Read More
204-Count-Primes
https://leetcode.com/problems/count-primes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countPrimes(self, n: int) -> int:
if n <= 2:
return 0
isPrime = [False] * 2 + [True] * (n - 2)
for i in range(2, int(n**0.5) + 1):
if isPrime …
Read More