發表文章

目前顯示的是 12月, 2021的文章

Leetcode 1015. Smallest Integer Divisible by K: 最小只有1被k整除的長度

  1015 .  Smallest Integer Divisible by K  python 解法 Given a positive integer  k , you need to find the  length  of the  smallest  positive integer  n  such that  n  is divisible by  k , and  n  only contains the digit  1 . Return  the  length  of  n . If there is no such  n , return -1. Note:   n  may not fit in a 64-bit signed integer. 解法: 數論: 按照定義寫下餘式的recursion 參見python代碼如下 注意 k至多只有k種不同的remainder 所以超過k次以後 就不用再測試了。 class Solution : def smallestRepunitDivByK ( self , k: int ) -> int : if k % 2 == 0 : return - 1 re = 0 for n in range ( 1 ,k + 1 ): re = (re * 10 + 1 ) % k if re == 0 : return n return - 1

Leetcode 刷題筆記: 688. Knight Probability in Chessboard (騎士跳出去的機率)

圖片
Leetcode 刷題筆記: 688. Knight Probability in Chessboard On an  n x n  chessboard, a knight starts at the cell  (row, column)  and attempts to make exactly  k  moves. The rows and columns are  0-indexed , so the top-left cell is  (0, 0) , and the bottom-right cell is  (n - 1, n - 1) . A chess knight has eight possible moves it can make, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction. Each time the knight is to move, it chooses one of eight possible moves uniformly at random (even if the piece would go off the chessboard) and moves there. The knight continues moving until it has made exactly  k  moves or has moved off the chessboard. Return  the probability that the knight remains on the board after it has stopped moving . Constraints: 1 <= n <= 25 0 <= k <= 100 0 <= row, column <= n 題目很清楚,給你一個knight開始的位置,然後開始跳,問k steps以後,騎士尚未離開board的機率 最簡單解法就是 定義 dp[i...