未验证 提交 7b6cab83 编写于 作者: S SherlockGuo 提交者: GitHub

【300.最长上升子序列】【Python】 (#507)

* add python

* add python

* add python
Co-authored-by: NLockyGuo <gh19920419@gmail.com>
Co-authored-by: Nlabuladong <labuladong@foxmail.com>
上级 424f210d
......@@ -217,6 +217,51 @@ public int lengthOfLIS(int[] nums) {
======其他语言代码======
```python 动态规划
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
n = len(nums)
f = [1] * (n)
for i in range(n):
for j in range(i):
if nums[j] < nums[i]:
f[i] = max(f[i], f[j] + 1)
res = 0
for i in range(n):
res = max(res, f[i])
return res
```
```python 二分查找
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
stack = []
def find_index(num):
l, r = 0, len(stack)
while l < r:
mid = l + r >> 1
if stack[mid] >= num:
r = mid
else:
l = mid + 1
return r
for num in nums:
if not stack or num > stack[-1]:
stack.append(num)
else:
position = find_index(num)
stack[position] = num
return len(stack)
```
[Kian](https://github.com/KianKw/) 提供 C++ 代码
```c++
......@@ -257,3 +302,4 @@ public:
}
};
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册