227-Basic-Calculator-Ii

Fri 14 November 2025

https://leetcode.com/problems/basic-calculator-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

228-Summary-Ranges

Fri 14 November 2025

https://leetcode.com/problems/summary-ranges

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

229-Majority-Element-Ii

Fri 14 November 2025

https://leetcode.com/problems/majority-element-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

23-Merge-K-Sorted-Lists

Fri 14 November 2025

https://leetcode.com/problems/merge-k-sorted-lists

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

230-Kth-Smallest-Element-In-A-Bst

Fri 14 November 2025

https://leetcode.com/problems/kth-smallest-element-in-a-bst

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

231-Power-Of-Two

Fri 14 November 2025

https://leetcode.com/problems/power-of-two

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def isPowerOfTwo(self, n: int) -> bool:
    return False if n < 0 else bin(n).count('1') == 1
new Solution().isPowerOfTwo()

Score: 5

Category: leetcode

Read More

233-Number-Of-Digit-One

Fri 14 November 2025

https://leetcode.com/problems/number-of-digit-one

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

234-Palindrome-Linked-List

Fri 14 November 2025

https://leetcode.com/problems/palindrome-linked-list

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

235-Lowest-Common-Ancestor-Of-A-Binary-Search-Tree

Fri 14 November 2025

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"))
from typing import List
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 …

Category: leetcode

Read More

236-Lowest-Common-Ancestor-Of-A-Binary-Tree

Fri 14 November 2025

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More
Page 18 of 77

« Prev Next »