This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def decodeString(self, s: str) -> str: | |
stack = [] | |
for i in s: | |
if i != "]": | |
stack.append(i) | |
else: | |
substring = "" | |
while stack[-1] != "[": |
NewerOlder