题目一
解法
class Solution {
public int longestValidParentheses(String s) {
int maxans = 0;
Deque<Integer> stack = new LinkedList<Integer>();
stack.push(-1);
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
stack.push(i);
} else {
stack.pop();
if (stack.isEmpty()) {
stack.push(i);
} else {
maxans = Math.max(maxans, i - stack.peek());
}
}
}
return maxans;
}
}
题目二
解法
class Solution {
public int kthToLast(ListNode head, int k) {
ListNode pre = head, cur = head;
for (int i = 0; i < k; i++)
cur = cur.next;
while (cur != null) {
pre = pre.next;
cur = cur.next;
}
return pre.val;
}
}