Re: [閒聊] 每日leetcode
※ 引述《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
討論串 (同標題文章)