標籤為 “Leetcode” 的頁面如下
Post
Grind 75 Python 做題記錄 53. Maximum Subarray
Grind 75 Python 做題記錄 53. Maximum Subarray Grind 75 連結 Leetcode 題目連結 實測起來if比max快一些。 import math class Solution: def maxSubArray(self, nums: List[int]) -> int: # Kadane's Algorithm 卡登算法 # 定義全局最大和與局部最大和 global_max_sum = -math.inf local_max_sum = 0
Post
Grind 75 Python 做題記錄 232. Implement Queue using Stacks
Grind 75 Python 做題記錄 232. Implement Queue using Stacks Grind 75 連結 Leetcode 題目連結 class MyQueue: def __init__(self): self.stack1 = list() self.stack2 = list() def push(self, x: int) -> None: # Push 的時候放入 Stack1 self.stack1.append(x) def pop(self) -> int: # Pop 的時候,如果 Stack2 存在元素,先從 Stack2 倒出 if
Post
Grind 75 Python 做題記錄 110. Balanced Binary Tree
Grind 75 Python 做題記錄 110. Balanced Binary Tree Grind 75 連結 Leetcode 題目連結 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isBalanced(self, root: Optional[TreeNode]) -> bool: # 深度優先搜索函數 def dfs(root: Optional[TreeNode]): # 如
Post
Grind 75 Python 做題記錄 141. Linked List Cycle
Grind 75 Python 做題記錄 141. Linked List Cycle Grind 75 連結 Leetcode 題目連結 # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def hasCycle(self, head: Optional[ListNode]) -> bool: # 快慢指針 fast = slow = head # 快針未觸底 while fast and fast.next: slow =
Post
Grind 75 Python 做題記錄 235. Lowest Common Ancestor of a Binary Search Tree
Grind 75 Python 做題記錄 235. Lowest Common Ancestor of a Binary Search Tree Grind 75 連結 Leetcode 題目連結 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': # 如果走到底仍
Post
Grind 75 Python 做題記錄 733. Flood Fill
Grind 75 Python 做題記錄 733. Flood Fill Grind 75 連結 Leetcode 題目連結 class Solution: def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]: # 取得寬度與高度 row_len:int = len(image) col_len:int = len(image[0]) # 取得起點顏色 origin_color = image[sr][sc] # 如果起點顏色為目標顏色,
Post
Grind 75 Python 做題記錄 704. Binary Search
Grind 75 Python 做題記錄 704. Binary Search Grind 75 連結 Leetcode 題目連結 class Solution: def search(self, nums: List[int], target: int) -> int: # 二分搜索的左右指標設定 left = 0 right = len(nums)-1 # 建立循環條件 while left <= right: # 計算中央值 mid = (left + right)//2 #
Post
Grind 75 Python 做題記錄 242. Valid Anagram
Grind 75 Python 做題記錄 242. Valid Anagram Grind 75 連結 Leetcode 題目連結 class Solution: def isAnagram(self, s: str, t: str) -> bool: # 假如字串長度不同,回傳 False if len(s) != len(t): return False # 假如轉換成集合不同,回傳 False s_set:set = set(s) if s_set != set(t): return
Post
Grind 75 Python 做題記錄 226. Invert Binary Tree
Grind 75 Python 做題記錄 226. Invert Binary Tree Grind 75 連結 Leetcode 題目連結 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: # 假如傳入節點為空,終止遞迴
Post
Grind 75 Python 做題記錄 125. Valid Palindrome
Grind 75 Python 做題記錄 125. Valid Palindrome Grind 75 連結 Leetcode 題目連結 class Solution: def isPalindrome(self, s: str) -> bool: # 取得統一小寫,並且去除標點的字串 clean_string : str = "".join(char.lower() for char in s if char.isalnum()) # 檢查字串是否等於反轉後的字串
Post
Grind 75 Python 做題記錄 121. Best Time to Buy and Sell Stock
Grind 75 Python 做題記錄 121. Best Time to Buy and Sell Stock Grind 75 連結 Leetcode 題目連結 import math class Solution: def maxProfit(self, prices: List[int]) -> int: # 定義最小價格 min_price:int = math.inf # 定義最大收益 max_profix:int = 0 # 遍歷價格 for current_price in prices: # 假如當前是最
Post
Grind 75 Python 做題記錄 21. Merge Two Sorted Lists
Grind 75 Python 做題記錄 21. Merge Two Sorted Lists Grind 75 連結 Leetcode 題目連結 # 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_head = ListNode() # 當前頭部節點 current_node
Post
Grind 75 Python 做題記錄 20. Valid Parentheses
Grind 75 Python 做題記錄 20. Valid Parentheses Grind 75 連結 Leetcode 題目連結 class Solution: def isValid(self, s: str) -> bool: # 若為空字串直接返回 if len(s) == 0: return True # 建立括弧對的紀錄 pari_dict:dict = { ")":"(", "}":"{", "]":"[" } # 建立 stack stack = list() # 遍歷字
Post
Grind 75 Python 做題記錄 1. Two Sum
Grind 75 Python 做題記錄 1. Two Sum Grind 75 連結 Leetcode 題目連結 # enumerate class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: # 建立快取 key 為數值差值,Value 為 index cache: dict = dict() # 遍歷 nums for k,v in enumerate(nums): # 找到差值 diff:int = target
Post
Grind 75 Python 做題記錄 67. Add Binary
Grind 75 Python 做題記錄 67. Add Binary Grind 75 連結 Leetcode 題目連結 位元加法 class Solution: def addBinary(self, a: str, b: str) -> str: # 初始化結果列表、進位(carry)和指標(i, j) result = [] carry = 0 i, j = len(a) - 1,
Post
Grind 75 Python 做題記錄 169. Majority Element
Grind 75 Python 做題記錄 169. Majority Element Grind 75 連結 Leetcode 題目連結 import collections class Solution: def majorityElement(self, nums: List[int]) -> int: # Boyer-Moore 算法 # 記數 count = 0 # 候選人 can = None # 遍歷 List for num in nums: # 假如記數為 0,則記錄候選人 if count
Post
Grind 75 Python 做題記錄 206. Reverse Linked List
Grind 75 Python 做題記錄 206. Reverse Linked List Grind 75 連結 Leetcode 題目連結 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: # 前一個指標 prev = None # 當前指標 curr = head # 遍歷串
Post
Grind 75 Python 做題記錄 409. Longest Palindrome
Grind 75 Python 做題記錄 409. Longest Palindrome Grind 75 連結 Leetcode 題目連結 import collections class Solution: def longestPalindrome(self, s: str) -> int: # 用 collections.Counter(s) 取得每個字元出現次數建立成 dict() # 用 .values() 取得值 # 並且只取出現為奇數的字元 # 計算奇數
Post
Grind 75 Python 做題記錄 70. Climbing Stairs
Grind 75 Python 做題記錄 70. Climbing Stairs Grind 75 連結 Leetcode 題目連結 class Solution: def climbStairs(self, n: int) -> int: # 記錄每一層樓梯的步驟數 way_dict = dict() # 初始值 way_dict[0] = 0 way_dict[1] = 1 # 走到二階有一次一階與一次兩階 way_dict[2] = 2
Post
Grind 75 Python 做題記錄 383. Ransom Note
Grind 75 Python 做題記錄 383. Ransom Note Grind 75 連結 Leetcode 題目連結 答題 class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: # 將重複字串過濾,變成一個不重複list r_list = list(set(ransomNote)) # 遍歷 List for i in r_list: # 檢查每個字字數
Post
Grind 75 Python 做題記錄 278. First Bad Version
Grind 75 Python 做題記錄 278. First Bad Version Grind 75 連結 Leetcode 題目連結 答題 # The isBadVersion API is already defined for you. # def isBadVersion(version: int) -> bool: class Solution: def firstBadVersion(self, n: int) -> int: # 最初版本號 left = 1 # 最終版本號 right = n # 二分搜索條件
Post
Grind 75 Python 做題記錄 543. Diameter of Binary Tree
Grind 75 Python 做題記錄 543. Diameter of Binary Tree Grind 75 連結 Leetcode 題目連結 答題 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: # 儲存當前最大寬度 self.max_diameter =
Post
Grind 75 Python 做題記錄 104. Maximum Depth of Binary Tree
Grind 75 Python 做題記錄 104. Maximum Depth of Binary Tree Grind 75 連結 Leetcode 題目連結 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: # 觸底返 0 if not root: return 0 # 取
Post
Grind 75 Python 做題記錄 876. Middle of the Linked List
Grind 75 Python 做題記錄 876. Middle of the Linked List Grind 75 連結 Leetcode 題目連結 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]: # 建立快慢針 fast = slow = head # 快針遍歷,快針
Post
Grind 75 Python 做題記錄 217. Contains Duplicate
Grind 75 Python 做題記錄 217. Contains Duplicate Grind 75 連結 Leetcode 題目連結 答題 class Solution: def containsDuplicate(self, nums: List[int]) -> bool: # Set 長度與字串長度不同,代表有重複 return len(set(nums)) != len(nums)