Re: [閒聊] 每日leetcode

看板Marginalman作者 (南爹摳打)時間1周前 (2024/04/24 09:39), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串154/187 (看更多)
※ 引述《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#了嗚哇啊啊啊 -- (づ′・ω・)づ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.96.37 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1713922798.A.640.html
文章代碼(AID): #1cA6BkP0 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cA6BkP0 (Marginalman)