Merge pull request #2735 from ChemGitXy/master

新增Cangjie解题代码
This commit is contained in:
程序员Carl
2024-09-26 13:33:10 +08:00
committed by GitHub
4 changed files with 80 additions and 0 deletions

View File

@ -337,6 +337,29 @@ pub fn length_of_lis(nums: Vec<i32>) -> i32 {
}
```
### Cangjie:
```cangjie
func lengthOfLIS(nums: Array<Int64>): Int64 {
let n = nums.size
if (n <= 1) {
return n
}
let dp = Array(n, item: 1)
var res = 0
for (i in 1..n) {
for (j in 0..i) {
if (nums[i] > nums[j]) {
dp[i] = max(dp[i], dp[j] + 1)
}
}
res = max(dp[i], res)
}
return res
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -492,6 +492,25 @@ int findLengthOfLCIS(int* nums, int numsSize) {
}
```
### Cangjie
```cangjie
func findLengthOfLCIS(nums: Array<Int64>): Int64 {
let n = nums.size
if (n <= 1) {
return n
}
let dp = Array(n, repeat: 1)
var res = 0
for (i in 1..n) {
if (nums[i] > nums[i - 1]) {
dp[i] = dp[i - 1] + 1
}
res = max(res, dp[i])
}
return res
}
```

View File

@ -581,6 +581,25 @@ int findLength(int* nums1, int nums1Size, int* nums2, int nums2Size) {
}
```
### Cangjie
```cangjie
func findLength(nums1: Array<Int64>, nums2: Array<Int64>): Int64 {
let n = nums1.size
let m = nums2.size
let dp = Array(n + 1, {_ => Array(m + 1, item: 0)})
var res = 0
for (i in 1..=n) {
for (j in 1..=m) {
if (nums1[i - 1] == nums2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1
}
res = max(res, dp[i][j])
}
}
return res
}
```
<p align="center">

View File

@ -399,6 +399,25 @@ int longestCommonSubsequence(char* text1, char* text2) {
}
```
### Cangjie
```cangjie
func longestCommonSubsequence(text1: String, text2: String): Int64 {
let n = text1.size
let m = text2.size
let dp = Array(n + 1, {_ => Array(m + 1, repeat: 0)})
for (i in 1..=n) {
for (j in 1..=m) {
if (text1[i - 1] == text2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
}
}
}
return dp[n][m]
}
```
<p align="center">