# leetcode-master **Repository Path**: pawpaw2022/leetcode-master ## Basic Information - **Project Name**: leetcode-master - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-05-29 - **Last Updated**: 2024-07-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # leetcode-master ## [6. Zigzag Conversion](https://leetcode.com/problems/zigzag-conversion/description/?envType=study-plan-v2&envId=top-interview-150) ### Explaination https://www.youtube.com/watch?v=fxhPxvBAvD0 ### Solution ```python class Solution: def convert(self, s: str, numRows: int) -> str: if numRows == 1 or numRows >= len(s): return s rows = [[] for _ in range(numRows)] idx, d = 0, 1 for char in s: rows[idx].append(char) if idx == 0: d = 1 elif idx == len(rows)-1: d = -1 idx += d res = "" for row in rows: res_row = "".join(row) res += res_row return res ``` --- ## [26. Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/?envType=study-plan-v2&envId=top-interview-150) ### Explaination None ### Solution ```python class Solution: def removeDuplicates(self, nums: List[int]) -> int: p1 = 0 # unique num pointer for p in range(len(nums)): if nums[p1] != nums[p]: p1 += 1 nums[p1] = nums[p] return p1 + 1 ``` --- ## [80. Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/?envType=study-plan-v2&envId=top-interview-150) ### Explaination None ### Solution ```python class Solution: def removeDuplicates(self, nums: List[int]) -> int: p1 = 1 count = 1 for p in range(1, len(nums)): if nums[p] == nums[p-1]: count += 1 else: count = 1 if count <= 2: nums[p1] = nums[p] p1 += 1 return p1 ``` --- ## [189. Rotate Array](https://leetcode.com/problems/rotate-array/description/?envType=study-plan-v2&envId=top-interview-150) ### Explaination None ### Solution ```python class Solution: def rotate(self, nums: List[int], k: int) -> None: """ Do not return anything, modify nums in-place instead. """ n = len(nums) # n -> length k %= n # k -> steps self.reverse(nums, 0, n-1) self.reverse(nums, 0, k-1) self.reverse(nums, k, n-1) def reverse(self, nums: List[int], start: int, end: int) -> None: while start < end: nums[start], nums[end] = nums[end], nums[start] start, end = start+1, end-1 ``` --- ## [125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/?envType=study-plan-v2&envId=top-interview-150) ### Explaination None ### Solution ```python class Solution: def isPalindrome(self, s: str) -> bool: if s == " ": return True # preprocessing seq = '' s = s.lower() for l in s: if l.isnumeric() or l.isalpha(): seq += l # validating palindrome start, end = 0, len(seq)-1 while start < end: if seq[start] != seq[end]: return False start, end = start+1, end-1 return True ``` --- ## [392. Is Subsequence](https://leetcode.com/problems/is-subsequence/description/?envType=study-plan-v2&envId=top-interview-150) ### Explaination None ### Solution ```python class Solution: def isSubsequence(self, s: str, t: str) -> bool: s_ptr, t_ptr = 0, 0 while s_ptr < len(s) and t_ptr < len(t): if s[s_ptr] == t[t_ptr]: s_ptr += 1 t_ptr += 1 return s_ptr == len(s) ``` --- ## [36. Valid Sudoku](https://leetcode.com/problems/valid-sudoku/description/?envType=study-plan-v2&envId=top-interview-150) ### Explaination None ### Solution ```python class Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: row = [set() for _ in range(9)] col = [set() for _ in range(9)] mx = [[set() for _ in range(3)] for _ in range(3)] for r in range(9): for c in range(9): cur = board[r][c] if cur == ".": continue # check row if cur in row[c]: return False else: row[c].add(cur) if cur in col[r]: return False else: col[r].add(cur) if cur in mx[r//3][c//3]: return False else: mx[r//3][c//3].add(cur) return True ``` --- ## [54. Spiral Matrix](YOUR_URL) ### Explaination None ### Solution ```python ``` --- ## [Question](YOUR_URL) ### Explaination None ### Solution ```python ``` --- ## [Question](YOUR_URL) ### Explaination None ### Solution ```python ``` --- ## [Question](YOUR_URL) ### Explaination None ### Solution ```python ``` --- ## [Question](YOUR_URL) ### Explaination None ### Solution ```python ``` --- ## [Question](YOUR_URL) ### Explaination None ### Solution ```python ``` --- ## [Question](YOUR_URL) ### Explaination None ### Solution ```python ``` --- ## [Question](YOUR_URL) ### Explaination None ### Solution ```python ``` --- ## [Question](YOUR_URL) ### Explaination None ### Solution ```python ``` --- ## [Question](YOUR_URL) ### Explaination None ### Solution ```python ``` --- ## [Question](YOUR_URL) ### Explaination None ### Solution ```python ``` --- ## [Question](YOUR_URL) ### Explaination None ### Solution ```python ``` --- ## [Question](YOUR_URL) ### Explaination None ### Solution ```python ``` --- ## [Question](YOUR_URL) ### Explaination None ### Solution ```python ``` --- ## [Question](YOUR_URL) ### Explaination None ### Solution ```python ```