Skip to content

Instantly share code, notes, and snippets.

View hongtaoh's full-sized avatar

Hongtao Hao hongtaoh

View GitHub Profile
@hongtaoh
hongtaoh / soln_lc415.py
Created October 9, 2024 22:44
Solution to Leetcode 415 Add Strings
class Solution:
# credit: https://www.youtube.com/watch?v=q1RR8gk47Cg
def addStrings(self, num1: str, num2: str) -> str:
i = len(num1) - 1
j = len(num2) - 1
carry = 0
res = []
while i >= 0 or j >= 0:
int1 = int(num1[i]) if i >=0 else 0
int2 = int(num2[j]) if j >= 0 else 0
@hongtaoh
hongtaoh / soln_lc21.py
Created October 9, 2024 23:33
Solution to Leetcode 21 Merge Two Sorted List
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode()
tail = dummy
while list1 and list2:
@hongtaoh
hongtaoh / soln_lc23.py
Created October 10, 2024 00:25
Solution to Leetcode 23 Merge k Sorted Lists
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
# credit: https://www.youtube.com/watch?v=q5a5OiGbT6Q
if not lists or len(lists) == 0:
return None
@hongtaoh
hongtaoh / soln_lc1768.py
Created October 25, 2024 16:53
Solution to Leetcode 1768
class Solution:
"""
This is okay but unnecessarily complicated.
if a = [1, 2, 3], then a[4:] won't cause any errors. This will help me get a better solution.
"""
def mergeAlternately(self, word1: str, word2: str) -> str:
# len1 < len2
len1, len2 = (len(word1), len(word2)) if len(word1) < len(word2) else (len(word2), len(word1))
output = []
@hongtaoh
hongtaoh / soln_lc283.py
Created October 26, 2024 21:59
Solution to Leetcode 283
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
l = 0
for r in range(len(nums)):
if nums[r] != 0:
nums[l], nums[r]= nums[r], nums[l]
l += 1