开学周 真的好忙呀啊啊
32:
最长有效括号 想着要用动态规划却不会用。。。
抄别人方法
class Solution(object): def longestValidParentheses(self, s): """ :type s: str :rtype: int """ n = len(s) stack = [] mark = [0]*n for i in range(n): if s[i] == '(': stack.append(i) else: if stack == []: mark[i] = 1 else: stack.pop() while stack != []: mark[stack.pop()] = 1 leng = 0 maxlen = 0 for i in mark: if i == 0: leng += 1 else: if leng>maxlen: maxlen = leng leng = 0 if leng>maxlen: maxlen = leng return maxlen 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/longest-valid-parentheses/solution/chao-bie-ren-de-he-ge-da-0-bu-he-ge-da-1-1jlk/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
接下来是几个二分查找 麻烦之处都在边界
33:
class Solution(object): def search(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ l = 0 r = len(nums)-1 while (l<=r): mid =( l + r) //2 if nums[mid] == target: return mid if nums[mid]>= nums[l]: if target >= nums[l] and target <= nums[mid]: r = mid -1 else: l = mid +1 else: if target >= nums[mid] and target <= nums[r]: l = mid +1 else: r = mid -1 return -1 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/search-in-rotated-sorted-array/solution/bian-jie-tiao-jian-nan-gao-qing-by-yizhu-3kze/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
34:超级容易越界 不行就try吧
class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ l = 0 r = len(nums)-1 rel = [-1,-1] while(l <= r): mid = (l+r)//2 if nums[mid] == target: rel[0] = mid rel[1] = mid if rel[0] > 0: while nums[rel[0]-1] == target : rel[0] -= 1 if rel[0] == 0: break if rel[1]+1 < len(nums) : while nums[rel[1]+1] == target : rel[1] += 1 if rel[1]+1 == len(nums) : break return rel if nums[mid] < target: l = mid +1 else: r = mid -1 return rel 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/solution/ru-he-jie-jue-yue-jie-wen-ti-cheng-liao-agekc/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
35:
搜索插入位置
二分法击败9成人
def searchInsert(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ l = 0 r = len(nums)-1 while(l <= r): mid = (l+r)//2 if nums[mid] == target: return mid if target > nums[mid] : l = mid +1 else: r = mid -1 return l
36:有效的数独
直接暴力解
class Solution(object): def isValidSudoku(self, board): """ :type board: List[List[str]] :rtype: bool """ dictnum = {str(i):0 for i in range(10)} #先判断行 for row in board: for each in row: if each != '.': dictnum[each] += 1 if dictnum[each] == 2: return False for num in dictnum: dictnum[num] = 0 # 判断列 for i in range(9): for row in board: if row[i] != '.': dictnum[row[i]] += 1 if dictnum[row[i]] == 2: return False for num in dictnum: dictnum[num] = 0 # 判断九格 for row in range(0,9,3): for col in range(0,9,3): for rows in board[row:row+3]: for each in rows[col:col+3]: if each != '.': dictnum[each] += 1 if dictnum[each] == 2: return False for num in dictnum: dictnum[num] = 0 return True 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/valid-sudoku/solution/bao-li-jie-fa-ye-neng-chao-guo-9cheng-de-wki1/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

浙公网安备 33010602011771号