iT邦幫忙

0

leetcode 365天 #Day115

  • 分享至 

  • xImage
  •  

中間發呆了一下的日子
Yes
未來應該會把我所寫過的題目正式錄個過程以及解釋,然後加入到播放清單中,要不然沒有紀錄。
有興趣的以後可以期待一下(?


  1. Strictly Palindromic Number (easy)
    https://leetcode.com/problems/strictly-palindromic-number/
    Submission:https://leetcode.com/problems/strictly-palindromic-number/submissions/856832672/
    An integer n is strictly palindromic if, for every base b between 2 and n - 2 (inclusive), the string representation of the integer n in base b is palindromic.
    Given an integer n, return true if n is strictly palindromic and false otherwise.
    A string is palindromic if it reads the same forward and backward.

一言難盡的題目

class Solution:
    #判斷n在 base:2 ~ n-2是否全部都為回文
    #幹,我就知道,我在怎麼想都想不出那可能的數字,靠杯,害我卡這麼久,幹
    #如果是2~n-1的話可能就需要思考
    #因為 n = (1) + (n-1),所以可能需要計算
    def isStrictlyPalindromic(self, n: int) -> bool:
        return 0

  1. Subrectangle Queries (medium)
    https://leetcode.com/problems/subrectangle-queries/
    Submission:https://leetcode.com/problems/subrectangle-queries/submissions/856823619/

Implement the class SubrectangleQueries which receives a rows x cols rectangle as a matrix of integers in the constructor and supports two methods:

  1. updateSubrectangle(int row1, int col1, int row2, int col2, int newValue)

Updates all values with newValue in the subrectangle whose upper left coordinate is (row1,col1) and bottom right coordinate is (row2,col2).
2. getValue(int row, int col)

Returns the current value of the coordinate (row,col) from the rectangle.
簡單來說,會丟一個矩陣出來,後面可能會調用api修改此矩陣,也可能會調用api讀取裡面的值

#一開始的寫法
class SubrectangleQueries:
    #重點是每筆測資最多會有500次指令,所以時間複雜度需要注意
    def __init__(self, rectangle: List[List[int]]):
        self.matrix = rectangle


    def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int, newValue: int) -> None:
        #把matrix[row1][col1] ~ matrix[row2][col2]的位置的值改成newValue
        temp = [newValue]*(col2-col1+1)
        for i in range(row1,row2+1):
            self.matrix[i][col1:col2+1] = temp

    def getValue(self, row: int, col: int) -> int: #獲取 matrix[row][col]
        return self.matrix[row][col]
#較優寫法,參考別人
#有時候想法不可以太過直接==
class SubrectangleQueries:
    #重點是每筆測資最多會有500次指令,所以時間複雜度需要注意
    def __init__(self, rectangle: List[List[int]]):
        self.matrix = rectangle
        self.rec = []
    def updateSubrectangle(self, row1: int, col1: int, row2: int, col2: int, newValue: int) -> None:
        self.rec.append([row1,col1,row2,col2,newValue])
        #紀錄所有的指令,因此這邊會變成O(1)
    def getValue(self, row: int, col: int) -> int: 
        #檢查要get的東西是否在下過指令的範圍,是的話回傳該數字,最多O(update的次數)
        for i in range(-1,-len(self.rec)-1,-1):
            if self.rec[i][0] <= row <= self.rec[i][2] and self.rec[i][1] <= col <=self.rec[i][3]:
                return self.rec[i][-1]
        return self.matrix[row][col]

  1. Missing Number In Arithmetic Progression (medium)
    https://leetcode.com/problems/missing-number-in-arithmetic-progression/
    Submission:https://leetcode.com/problems/missing-number-in-arithmetic-progression/submissions/856811351/

In some array arr, the values were in arithmetic progression: the values arr[i + 1] - arr[i] are all equal for every 0 <= i < arr.length - 1.

A value from arr was removed that was not the first or last value in the array.

Given arr, return the removed value.
題目敘述在程式碼裡。
一開始的寫法,有點囉嗦

class Solution:
    #找出等差數列裡面所缺少的那個數字
    #所缺少的那個數字不會是最後或最前面的數字
    def missingNumber(self, arr: List[int]) -> int:
        d1,d2 = arr[1] - arr[0],arr[2] - arr[1]
        flag = 0
        if d1 < 0:
            flag = 1
        elif d1 == 0:#若公差為0則必缺少同一種數字
            return arr[0]
        realD = min(abs(d1),abs(d2))#我這邊還要判斷正負d所以多了耗能
        if flag:
            realD = -realD
        temp = arr[0]
        while 1:
            temp += realD
            if temp not in arr:
                return temp
class Solution:
    #別人的寫法
    #較優的寫法
    def missingNumber(self, arr: List[int]) -> int:
        diff = (arr[-1] - arr[0])//len(arr)
        #因為最後一個跟第0個不會被捨棄,故這樣算一定可以得到正確的d
        if diff==0:
            return arr[0]
        for i in range(1, len(arr)):
            if arr[i-1] + diff != arr[i]:
                return arr[i-1] + diff

  1. Leaf-Similar Trees (easy)
    https://leetcode.com/problems/leaf-similar-trees/
    Submission:https://leetcode.com/problems/leaf-similar-trees/submissions/856806489/

Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence.

For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).

Two binary trees are considered leaf-similar if their leaf value sequence is the same.

Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar.
把root1和root2的葉節點找出來,比較順序是否一樣。

# 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:
    #給兩個tree,查詢此二tree從左至右的leaves所形成的串列是否相等(順序也要)。
    def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
        #如果同時好像不太方便,因為極有可能tree長的樣子不一樣,所以分開討論
        def dfs(root):
            if root.left is None and root.right is None:
                return [root.val]
            elif root.left is None:
                return dfs(root.right)
            elif root.right is None:
                return dfs(root.left)
            return dfs(root.left) + dfs(root.right)
        return dfs(root1) == dfs(root2)

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言