From 5f07ae3b033644dc6cbcf96c23eeef136ed4a5b0 Mon Sep 17 00:00:00 2001 From: a12bb <2713204748@qq.com> Date: Wed, 13 Mar 2024 22:00:54 +0800 Subject: [PATCH] =?UTF-8?q?Update=200300.=E6=9C=80=E9=95=BF=E4=B8=8A?= =?UTF-8?q?=E5=8D=87=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0300.最长上升子序列的新增C语言实现 --- problems/0300.最长上升子序列.md | 31 +++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/problems/0300.最长上升子序列.md b/problems/0300.最长上升子序列.md index 64f75291..6d82eae1 100644 --- a/problems/0300.最长上升子序列.md +++ b/problems/0300.最长上升子序列.md @@ -288,6 +288,36 @@ function lengthOfLIS(nums: number[]): number { }; ``` +### C: + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int lengthOfLIS(int* nums, int numsSize) { + if(numsSize <= 1){ + return numsSize; + } + int dp[numsSize]; + for(int i = 0; i < numsSize; i++){ + dp[i]=1; + } + int result = 1; + for (int i = 1; i < numsSize; ++i) { + for (int j = 0; j < i; ++j) { + if(nums[i] > nums[j]){ + dp[i] = max(dp[i], dp[j] + 1); + } + if(dp[i] > result){ + result = dp[i]; + } + } + } + return result; +} +``` + + + ### Rust: ```rust @@ -311,4 +341,3 @@ pub fn length_of_lis(nums: Vec) -> i32 { -