class Node: def init(self, data, next): self.data = data self.next = next
def remove_tail_nth_list(head, n): prev, cur, tail = head, head, head
i = 0
while tail is not None:
i += 1
if i == n:
break
tail = tail.next
if i < n:
return head
while tail.next is not None:
prev = cur
tail = tail.next
cur = cur.next
if prev == head:
return head.next
prev.next = prev.next.next
return head
def isValidParentheses(l): stack = [] for c in l: if c in (‘(‘, ‘[‘, ‘{‘): stack.append(c) elif c in (‘)’, ‘]’, ‘}’): if len(stack) == 0: return False top = stack.pop() if c == ‘)’ and top != ‘(‘: return False if c == ‘]’ and top != ‘[‘: return False if c == ‘}’ and top != ‘{‘: return False else: return False
if (ok) { // Calldata takes ownership of the completion queue inside sync_req SyncRequest::CallData cd(server_, sync_req); // Prepare for the next request if (!IsShutdown()) { sync_req->SetupRequest(); // Create new completion queue for sync_req sync_req->Request(server_->c_server(), server_cq_->cq()); }
deflongestPalindromeDp(s): dp = [[0]*len(s) for _ inrange(len(s))] left, right, length = 0, 0, 0 for i inrange(len(s)): for j inrange(i): if s[i] == s[j] and (i - j < 2or dp[j+1][i-1]): dp[j][i] = 1 if i - j + 1 > length: length = i - j + 1 left = j right = i dp[i][i] = 1 return s[left:right+1] if __name__ == "__main__": s = "abcdedcf" print(longestPalindromeDp(s))
defmax_sub_array(a): sum, max, start, new_start, end = a[0], a[0], 0, 0, 0 for i, e inenumerate(a[1:], 1): ifsum <= 0: new_start = i sum = e else: sum += e ifsum > max: start = new_start end = i max = sum return start, end, max