This commit is contained in:
2025-12-05 21:18:40 +08:00
parent 36c5339b1f
commit 727096b8e6
3 changed files with 67 additions and 0 deletions

14
include/solution/509.h Normal file
View File

@@ -0,0 +1,14 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#ifndef INC_509_H
#define INC_509_H
#ifdef __cplusplus
extern "C"
{
#endif
int fib(int n);
#ifdef __cplusplus
}
#endif
#endif // INC_509_H

14
src/509.c Normal file
View File

@@ -0,0 +1,14 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <solution/509.h>
int fib(int n)
{
int dp[n + 1];
dp[0] = 0;
if (n >= 1)
dp[1] = 1;
for (int i = 2; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2];
return dp[n];
}

39
tests/test_509.cpp Normal file
View File

@@ -0,0 +1,39 @@
// This file is generated by mkfile.py
// Date: 2025-12-05
#include <gtest/gtest.h>
#include <solution/509.h>
// Tests for fib (LeetCode 509)
class FibTest : public ::testing::Test
{
protected:
void AssertFib(int n, int expected)
{
ASSERT_EQ(fib(n), expected);
}
};
TEST_F(FibTest, Zero)
{
AssertFib(0, 0);
}
TEST_F(FibTest, One)
{
AssertFib(1, 1);
}
TEST_F(FibTest, Two)
{
AssertFib(2, 1);
}
TEST_F(FibTest, Ten)
{
AssertFib(10, 55);
}
TEST_F(FibTest, Twenty)
{
AssertFib(20, 6765);
}