Skip to content

Commit f2ac985

Browse files
committed
最长上升子序列
1 parent 513c17e commit f2ac985

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

longest_increasing_subsequence.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution:
4+
"""
5+
@param nums: The integer array
6+
@return: The length of LIS (longest increasing subsequence)
7+
"""
8+
def longestIncreasingSubsequence(self, nums):
9+
# write your code here
10+
ret = 0
11+
if nums:
12+
caches = [0] * len(nums) # 到当前位置最长子序列长度
13+
caches[0] = 1
14+
for i in xrange(1, len(nums)):
15+
_len = 0
16+
for j in xrange(i):
17+
if nums[j] < nums[i]:
18+
_len = max(caches[j], _len)
19+
caches[i] = _len + 1
20+
ret = max(caches[i], ret)
21+
return ret

0 commit comments

Comments
 (0)