Skip to content

Instantly share code, notes, and snippets.

View hongtaoh's full-sized avatar

Hongtao Hao hongtaoh

View GitHub Profile
@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
@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_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_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_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_lc34.py
Created October 9, 2024 22:20
Solution to Leetcode 34 Find First and Last Position of Element in Sorted Array
class Solution:
def searchRange(self, nums: List[int], target: int) -> List[int]:
left = self.binSearch(nums, target, True)
right = self.binSearch(nums, target, False)
return [left, right]
# modified binary search algorithm
def binSearch(self, nums, target, leftBiased):
l = 0
r = len(nums) - 1
@hongtaoh
hongtaoh / soln_lc70.py
Created October 9, 2024 16:10
Solution to Leetcode 70 Climbing Stairs
class Solution:
# dynamic programming
def climbStairs(self, n: int) -> int:
one, two = 1, 1
for _ in range(n-1):
temp = one
one += two
two = temp
return one
@hongtaoh
hongtaoh / soln_lc1071.py
Created October 9, 2024 15:03
Solution to Leetcode 1071 Greatest Common Divisor of Strings
class Solution:
# the most straightforward implementation
def gcdOfStrings(self, str1: str, str2: str) -> str:
short_str, long_str = (str1, str2) if len(str1) <= len(str2) else (str2, str1)
l = 0
while l < len(short_str):
r = len(short_str)
while r >l and r <= len(short_str):
substr = short_str[l:r]
substr_len = r - l
@hongtaoh
hongtaoh / soln_lc3.py
Created October 8, 2024 03:04
Solution to Leetcode 3 Longest Substring Without Repeating Characters
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
charSet = set()
left = 0
maxLen = 0
for right in range(len(s)):
while s[right] in charSet:
charSet.remove(s[left])
left += 1
charSet.add(s[right])
@hongtaoh
hongtaoh / soln_lc394.py
Created October 7, 2024 21:39
Solution to Leetcode 394 Decode String
class Solution:
def decodeString(self, s: str) -> str:
stack = []
for i in s:
if i != "]":
stack.append(i)
else:
substring = ""
while stack[-1] != "[":