Skip to content

Commit 1387c77

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0674
1 parent af1b1bd commit 1387c77

File tree

7 files changed

+73
-22
lines changed

7 files changed

+73
-22
lines changed

solution/0300-0399/0300.Longest Increasing Subsequence/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ class Solution:
4646
n = len(nums)
4747
if n < 2:
4848
return n
49-
dp = [1 for _ in nums]
49+
dp = [1] * n
5050
res = 1
5151
for i in range(1, n):
52-
for j in range(0, i):
52+
for j in range(i):
5353
if nums[j] < nums[i]:
5454
dp[i] = max(dp[i], dp[j] + 1)
5555
res = max(res, dp[i])

solution/0300-0399/0300.Longest Increasing Subsequence/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class Solution:
3737
n = len(nums)
3838
if n < 2:
3939
return n
40-
dp = [1 for _ in nums]
40+
dp = [1] * n
4141
res = 1
4242
for i in range(1, n):
43-
for j in range(0, i):
43+
for j in range(i):
4444
if nums[j] < nums[i]:
4545
dp[i] = max(dp[i], dp[j] + 1)
4646
res = max(res, dp[i])

solution/0300-0399/0300.Longest Increasing Subsequence/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ def lengthOfLIS(self, nums: List[int]) -> int:
33
n = len(nums)
44
if n < 2:
55
return n
6-
dp = [1 for _ in nums]
6+
dp = [1] * n
77
res = 1
88
for i in range(1, n):
9-
for j in range(0, i):
9+
for j in range(i):
1010
if nums[j] < nums[i]:
1111
dp[i] = max(dp[i], dp[j] + 1)
1212
res = max(res, dp[i])

solution/0600-0699/0674.Longest Continuous Increasing Subsequence/README.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,48 @@
3030

3131
<!-- 这里可写通用的实现逻辑 -->
3232

33+
设 f(i) 表示将数组第 i 项作为最长连续递增子序列的最后一项时,子序列的长度。
34+
35+
那么,当 `nums[i - 1] < nums[i]`,即 `f(i) = f(i - 1)` + 1,否则 `f(i) = 1`。问题转换为求 f(i) (`i ∈ [0 ,n - 1]`) 的最大值。
36+
37+
由于 f(i) 只与前一项 f(i - 1) 有关联,故不需要用一个数组存储。
38+
3339
<!-- tabs:start -->
3440

3541
### **Python3**
3642

3743
<!-- 这里可写当前语言的特殊实现逻辑 -->
3844

3945
```python
40-
46+
class Solution:
47+
def findLengthOfLCIS(self, nums: List[int]) -> int:
48+
n = len(nums)
49+
if n < 2:
50+
return n
51+
res = f = 1
52+
for i in range(1, n):
53+
f = 1 + (f if nums[i - 1] < nums[i] else 0)
54+
res = max(res, f)
55+
return res
4156
```
4257

4358
### **Java**
4459

4560
<!-- 这里可写当前语言的特殊实现逻辑 -->
4661

4762
```java
48-
63+
class Solution {
64+
public int findLengthOfLCIS(int[] nums) {
65+
int n;
66+
if ((n = nums.length) < 2) return n;
67+
int res = 1, f = 1;
68+
for (int i = 1; i < n; ++i) {
69+
f = 1 + (nums[i - 1] < nums[i] ? f : 0);
70+
res = Math.max(res, f);
71+
}
72+
return res;
73+
}
74+
}
4975
```
5076

5177
### **...**

solution/0600-0699/0674.Longest Continuous Increasing Subsequence/README_EN.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,33 @@ Length of the array will not exceed 10,000.
5353
### **Python3**
5454

5555
```python
56-
56+
class Solution:
57+
def findLengthOfLCIS(self, nums: List[int]) -> int:
58+
n = len(nums)
59+
if n < 2:
60+
return n
61+
res = f = 1
62+
for i in range(1, n):
63+
f = 1 + (f if nums[i - 1] < nums[i] else 0)
64+
res = max(res, f)
65+
return res
5766
```
5867

5968
### **Java**
6069

6170
```java
62-
71+
class Solution {
72+
public int findLengthOfLCIS(int[] nums) {
73+
int n;
74+
if ((n = nums.length) < 2) return n;
75+
int res = 1, f = 1;
76+
for (int i = 1; i < n; ++i) {
77+
f = 1 + (nums[i - 1] < nums[i] ? f : 0);
78+
res = Math.max(res, f);
79+
}
80+
return res;
81+
}
82+
}
6383
```
6484

6585
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
class Solution {
22
public int findLengthOfLCIS(int[] nums) {
3-
if (nums == null || nums.length == 0) {
4-
return 0;
5-
}
6-
int cnt = 1;
7-
int res = 1;
8-
for (int i = 1; i < nums.length; ++i) {
9-
if (nums[i] > nums[i - 1]) {
10-
res = Math.max(res, ++cnt);
11-
} else {
12-
cnt = 1;
13-
}
3+
int n;
4+
if ((n = nums.length) < 2) return n;
5+
int res = 1, f = 1;
6+
for (int i = 1; i < n; ++i) {
7+
f = 1 + (nums[i - 1] < nums[i] ? f : 0);
8+
res = Math.max(res, f);
149
}
1510
return res;
1611
}
17-
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def findLengthOfLCIS(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
if n < 2:
5+
return n
6+
res = f = 1
7+
for i in range(1, n):
8+
f = 1 + (f if nums[i - 1] < nums[i] else 0)
9+
res = max(res, f)
10+
return res

0 commit comments

Comments
 (0)