257-Binary-Tree-Paths
https://leetcode.com/problems/binary-tree-paths
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
ans = []
def dfs(root: Optional[TreeNode], path: List[str]) -> None:
if not root:
return
if not root.left and not root …
Read More
258-Add-Digits
https://leetcode.com/problems/add-digits
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def addDigits(self, num: int) -> int:
return 0 if num == 0 else 1 + (num - 1) % 9
new Solution().addDigits()
Score: 5
Read More
259-3Sum-Smaller
https://leetcode.com/problems/3sum-smaller
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def threeSumSmaller(self, nums: List[int], target: int) -> int:
if len(nums) < 3:
return 0
ans = 0
nums.sort()
for i in range(len(nums) - 2):
l = i + 1 …
Read More
26-Remove-Duplicates-From-Sorted-Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
i = 0
for num in nums:
if i < 1 or num > nums[i - 1]:
nums[i] = num
i += 1
return i
Read More
260-Single-Number-Iii
https://leetcode.com/problems/single-number-iii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
xors = functools.reduce(operator.xor, nums)
lowbit = xors & -xors
ans = [0, 0]
# Seperate nums into two groups by the lowbit
for …
Read More
261-Graph-Valid-Tree
https://leetcode.com/problems/graph-valid-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def validTree(self, n: int, edges: List[List[int]]) -> bool:
if n == 0 or len(edges) != n - 1:
return False
graph = [[] for _ in range(n)]
q = deque([0 …
Read More
263-Ugly-Number
https://leetcode.com/problems/ugly-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isUgly(self, n: int) -> bool:
if n == 0:
return False
for prime in 2, 3, 5:
while n % prime == 0:
n //= prime
return n == 1
Read More
264-Ugly-Number-Ii
https://leetcode.com/problems/ugly-number-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def nthUglyNumber(self, n: int) -> int:
nums = [1]
i2 = 0
i3 = 0
i5 = 0
while len(nums) < n:
next2 = nums[i2] * 2
next3 = nums[i3] * 3
next5 = nums[i5 …
Read More
265-Paint-House-Ii
https://leetcode.com/problems/paint-house-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minCostII(self, costs: List[List[int]]) -> int:
prevIndex = -1 # The previous minimum index
prevMin1 = 0 # Minimum cost so far
prevMin2 = 0 # 2nd minimum cost so far
for …
Read More
266-Palindrome-Permutation
https://leetcode.com/problems/palindrome-permutation
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canPermutePalindrome(self, s: str) -> bool:
seen = set()
for c in s:
if c in seen:
seen.remove(c)
else:
seen.add(c)
return len(seen) <= 1
Read More