# Python每日一练(20230428) 最长有效括号、矩阵最长递增路径、回文链表

## 1. 最长有效括号

• 0 <= s.length <= 3 * 10^4
• s[i]'('')'

python
class Solution(object):
def longestValidParentheses(self, s):
ls = len(s)
stack = []
data = [0] * ls
for i in range(ls):
curr = s[i]
if curr == '(':
stack.append(i)
else:
__________________;
tep, res = 0, 0
for t in data:
if t == 1:
tep += 1
else:
res = max(tep, res)
tep = 0
return max(tep, res)
if __name__ == '__main__':
s = Solution()
print(s.longestValidParentheses(')()())'))


https://edu.csdn.net/practice/26740290

class Solution(object):
def longestValidParentheses(self, s):
ls = len(s)
stack = []
data = [0] * ls
for i in range(ls):
curr = s[i]
if curr == '(':
stack.append(i)
else:
if len(stack) > 0:
data[i] = 1
data[stack.pop(-1)] = 1
tep, res = 0, 0
for t in data:
if t == 1:
tep += 1
else:
res = max(tep, res)
tep = 0
return max(tep, res)
if __name__ == '__main__':
s = Solution()
print(s.longestValidParentheses(')()())'))

4

## 2. 矩阵中的最长递增路径

• m == matrix.length
• n == matrix[i].length
• 1 <= m, n <= 200
• 0 <= matrix[i][j] <= 2^31 - 1

https://edu.csdn.net/practice/26740291

class Solution:
def longestIncreasingPath(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: int
"""
a = len(matrix)
dic = {}
nums_max = 1
if a == 0:
nums_max = 0
else:
b = len(matrix[0])
for i in range(a):
for j in range(b):
dic[(i,j)] = matrix[i][j]
v =  dic.keys()
nums1 = [[1 for i in range(b)] for j in range(a)]
dic = sorted(dic.items(),key = lambda x:x[1])
for k in dic:
i = k[0][0]
j = k[0][1]
if (i+1,j) in v and matrix[i+1][j]<matrix[i][j] and nums1[i][j]<nums1[i+1][j]+1:
nums1[i][j] = nums1[i+1][j] + 1
if (i,j+1) in v and matrix[i][j+1]<matrix[i][j] and nums1[i][j]<nums1[i][j+1]+1:
nums1[i][j] = nums1[i][j+1] +1
if (i-1,j) in v and matrix[i-1][j]<matrix[i][j] and nums1[i][j]<nums1[i-1][j]+1:
nums1[i][j] = nums1[i-1][j] +1
if (i,j-1) in v and matrix[i][j-1]<matrix[i][j] and nums1[i][j]<nums1[i][j-1]+1:
nums1[i][j] = nums1[i][j-1] + 1
nums_max = max(nums_max,nums1[i][j])
return nums_max
if __name__ == '__main__':
s = Solution()
matrix = [[9,9,4],[6,6,8],[2,1,1]]
print(s.longestIncreasingPath(matrix))
matrix = [[3,4,5],[3,2,6],[2,2,1]]
print(s.longestIncreasingPath(matrix))

4

4

## 3. 回文链表

• 链表中节点数目在范围[1, 10^5]
• 0 <= Node.val <= 9

https://edu.csdn.net/practice/26740292

class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
lst = []
while node:
lst.append(node.val)
node = node.next
start = 0
end = len(lst) - 1
while start < end:
if lst[start] != lst[end]:
return False
start += 1
end -= 1
return True
def createList(lst):
if not lst:
return None
for i in range(1, len(lst)):
curr.next = ListNode(lst[i])
curr = curr.next
if __name__ == '__main__':
s = Solution()
nums = [1,2,2,1]
nums = [1,2]
print(s.isPalindrome(head))

True

False

## 🌟 每日一练刷题专栏 🌟

👍 点赞，你的认可是我坚持的动力！

🌟 收藏，你的青睐是我努力的方向！

|
16天前
|
IDE 开发工具 Python
python语法中括号不匹配处理
【7月更文挑战第8天】
13 2
|
14天前
【数据结构OJ题】链表的回文结构

18 0
|
1月前
|

Python遍历矩阵的技巧与实践
Python遍历矩阵的技巧与实践
28 2
|
1月前
|

Python矩阵转灰度图技术解析
Python矩阵转灰度图技术解析
18 1
|
15天前
|
Python

10 0
|
1月前
|

24 4
|
1月前
|

26 2
|
1月前
|
SQL 算法 数据可视化
LeetCode题目92:反转链表ll 【python 递归与迭代方法全解析】
LeetCode题目92:反转链表ll 【python 递归与迭代方法全解析】
23 1
|
1月前
|

25 2
|
1月前
|

LeetCode 题目 62：不同路径【python】
LeetCode 题目 62：不同路径【python】
23 2