Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (caster )時間1年前 (2024/04/24 10:46), 編輯推噓2(201)
留言3則, 3人參與, 1年前最新討論串155/1548 (看更多)
※ 引述《SecondRun (南爹摳打)》之銘言: : ※ 引述《Rushia (早瀬ユウカの体操服 )》之銘言: : : https://leetcode.com/problems/n-th-tribonacci-number/description : : 1137. N-th Tribonacci Number : : 給你一個數字n,求出第 n 個 Tribonacci 數列是多少。 : : 思路: : : 1.動態規劃,然後把空間壓一壓。 : : ----------------------------------------- : : class Solution: : : def tribonacci(self, n: int) -> int: : : if n == 0: : : return 0 : : if n <= 2: : : return 1 : : n1, n2, n3 = 0, 1, 1 : : for i in range(3, n + 1): : : n1, n2, n3 = n2, n3, n1 + n2 + n3 : : return n3 : : ----------------------------------------- : C# code : public class Solution { : public int Tribonacci(int n) : { : if (n == 0) return 0; : if (n <= 2) return 1; : int[] nums = new int[3] {0,1,1}; : int result = 0; : for (int i=3; i<=n; i++) : { : result = nums.Sum(); : nums[0] = nums[1]; : nums[1] = nums[2]; : nums[2] = result; : } : return result; : } : } : 泥板剩我還在寫C#了嗚哇啊啊啊 Python Code: class Solution: @cache def tribonacci(self, n: int) -> int: if n ==0 or n == 1: return n if n == 2: return 1 return self.tribonacci(n-1) + self.tribonacci(n-2) + self.tribonacci(n-3) 裝飾器 啟動 面試應該不能用這招吧 下次乖乖寫dp -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.43.130.137 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1713926775.A.EA4.html

04/24 10:48, 1年前 , 1F
大師
04/24 10:48, 1F

04/24 10:50, 1年前 , 2F
大師
04/24 10:50, 2F

04/24 10:51, 1年前 , 3F
大師
04/24 10:51, 3F
文章代碼(AID): #1cA79twa (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cA79twa (Marginalman)