227-Basic-Calculator-Ii
https://leetcode.com/problems/basic-calculator-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def calculate(self, s: str) -> int:
ans = 0
prevNum = 0
currNum = 0
op = '+'
for i, c in enumerate(s):
if c.isdigit():
currNum = currNum * 10 + int(c)
if not …
Read More
228-Summary-Ranges
https://leetcode.com/problems/summary-ranges
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
ans = []
i = 0
while i < len(nums):
begin = nums[i]
while i < len(nums) - 1 and nums[i] == nums[i + 1 …
Read More
229-Majority-Element-Ii
https://leetcode.com/problems/majority-element-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def majorityElement(self, nums: List[int]) -> List[int]:
ans1 = 0
ans2 = 1
count1 = 0
count2 = 0
for num in nums:
if num == ans1:
count1 += 1
elif num == ans2:
count2 …
Read More
23-Merge-K-Sorted-Lists
https://leetcode.com/problems/merge-k-sorted-lists
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from queue import PriorityQueue
class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
dummy = ListNode(0)
curr = dummy
pq = PriorityQueue()
for i, lst in enumerate(lists):
if lst:
pq.put((lst …
Read More
230-Kth-Smallest-Element-In-A-Bst
https://leetcode.com/problems/kth-smallest-element-in-a-bst
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
def countNodes(root: Optional[TreeNode]) -> int:
if not root:
return 0
return 1 + countNodes(root.left) + countNodes(root.right)
leftCount …
Read More
231-Power-Of-Two
https://leetcode.com/problems/power-of-two
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
return False if n < 0 else bin(n).count('1') == 1
new Solution().isPowerOfTwo()
Score: 5
Read More
233-Number-Of-Digit-One
https://leetcode.com/problems/number-of-digit-one
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countDigitOne(self, n: int) -> int:
ans = 0
pow10 = 1
while pow10 <= n:
divisor = pow10 * 10
quotient = n // divisor
remainder = n % divisor
if quotient > 0:
ans += quotient * pow10
if …
Read More
234-Palindrome-Linked-List
https://leetcode.com/problems/palindrome-linked-list
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
def reverseList(head: ListNode) -> ListNode:
prev = None
curr = head
while curr:
next = curr.next
curr.next = prev
prev = curr
curr = next
return prev …
Read More
235-Lowest-Common-Ancestor-Of-A-Binary-Search-Tree
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if root.val > max(p.val, q.val):
return self.lowestCommonAncestor(root.left, p, q)
if root.val < min …
Read More
236-Lowest-Common-Ancestor-Of-A-Binary-Tree
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if not root or root == p or root == q:
return root
l = self.lowestCommonAncestor(root.left, p, q)
r …
Read More