while (head != null) { if (nodeSet.contains(head)) return head; nodeSet.add(head); head = head.next; }
returnnull; } }
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classListNode: def__init__(self, x): self.val = x self.next = None classCycleList: defhasCycle(self, head): node_set = set() while head: if head in node_set: return head node_set.add(head) head = head.next returnNone
classListNode: def__init__(self, x): self.val = x self.next = None
classCycleList: defhasCycle(self, head): if head and head.nextand head.next.next: slow = head.next fast = head.next.next while slow != fast: if fast.nextand fast.next.next: slow = slow.next fast = fast.next.next else: returnNone fast = head while slow != fast: slow = slow.next fast = fast.next return fast else: returnNone
classListNode: def__init__(self, x): self.val = x self.next = None
classCycleList: defhasCycle(self, head): if head and head.nextand head.next.next: slow = head.next fast = head.next.next while slow != fast: if fast.nextand fast.next.next: slow = slow.next fast = fast.next.next else: returnFalse returnTrue else: returnFalse
classNode: def__init__(self, x, next=None, random=None): self.val = int(x) self.next = next self.random = random
classCopyList: defcopy_random_list(self, head): if head isNone: returnNone tmp = head while tmp: new_node = Node(tmp.val) new_node.next = tmp.next tmp.next = new_node tmp = new_node.next tmp = head while tmp: new_node = tmp.next new_node.random = tmp.random.nextif tmp.random elseNone tmp = new_node.next tmp = head result = head.next while tmp: next = tmp.next.next new_node = tmp.next tmp.next = next new_node.next = next.nextifnextelseNone tmp = next return result
defget_edge(matrix: list) -> list: if row1 == row2: for i inrange(col1, col2 + 1): result.append(matrix[row1][i]) return
if col1 == col2: for i inrange(row1, row2 + 1): result.append(matrix[i][col1]) return
for i inrange(col1, col2): result.append(matrix[row1][i]) for i inrange(row1, row2): result.append(matrix[i][col2]) for i inrange(col2, col1, -1): result.append(matrix[row2][i]) for i inrange(row2, row1, -1): result.append(matrix[i][col1])
funcgetEdge(m [][]int, row1, col1, row2, col2 int) []int { var tmpResult []int if row1 == row2 { for i := col1; i <= col2; i++ { tmpResult = append(tmpResult, m[row1][i]) } return tmpResult } if col1 == col2 { for i := row1; i <= row2; i++ { tmpResult = append(tmpResult, m[i][col1]) } return tmpResult }
for i := col1; i < col2; i++ { tmpResult = append(tmpResult, m[row1][i]) } for i := row1; i < row2; i++ { tmpResult = append(tmpResult, m[i][col2]) } for i := col2; i > col1; i-- { tmpResult = append(tmpResult, m[row2][i]) } for i := row2; i > row1; i-- { tmpResult = append(tmpResult, m[i][col1]) }
classSolution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> result = newArrayList<>(); if (matrix == null || matrix.length == 0) return result; intl=0, r = matrix[0].length - 1; intt=0, b = matrix.length - 1;
while (l <= r && t <= b) { for (inti= l; i <= r; i++) result.add(matrix[t][i]); t++; for (inti= t; i <= b; i++) result.add(matrix[i][r]); r--; for (inti= r; i >= l && t <= b; i--) result.add(matrix[b][i]); b--; for (inti= b; i >= t && l <= r; i--) result.add(matrix[i][l]); l++; }
return result; } }
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution: defspiralOrder(self, matrix: list) -> list: result = [] ifnot matrix orlen(matrix) == 0: return result l, r, t, b = 0, len(matrix[0]) - 1, 0, len(matrix) - 1
while l <= r and t <= b: for i inrange(l, r + 1): result.append(matrix[t][i]) t += 1 for i inrange(t, b + 1): result.append(matrix[i][r]) r -= 1 if (l <= r and t <= b): for i inrange(r, l - 1, -1): result.append(matrix[b][i]) b -= 1 for i inrange(b, t - 1, -1): result.append(matrix[i][l]) l += 1
return result
利用 zip 函数:
1 2 3 4 5 6 7 8
classSolution: defspiralOrder(self, matrix: list) -> list: res = [] while matrix: res.extend(matrix[0]) matrix[:] = list(zip(*matrix[1:]))[::-1] return res
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
funcspiralOrder(matrix [][]int) []int { var result []int if matrix == nil || len(matrix) == 0 { return result }
for l <= r && t <= b { for i := l; i <= r; i++ { result = append(result, matrix[t][i]) } t++ for i := t; i <= b; i++ { result = append(result, matrix[i][r]) } r-- for i := r; i >= l && t <= b; i-- { result = append(result, matrix[b][i]) } b-- for i := b; i >= t && l <= r; i-- { result = append(result, matrix[i][l]) } l++ }
/** Initialize your data structure here. */ publicMyQueue() { pushStack = newStack<>(); popStack = newStack<>(); } /** Push element x to the back of queue. */ publicvoidpush(int x) { stackTransfer(popStack, pushStack); pushStack.push(x); } /** Removes the element from in front of queue and returns that element. */ publicintpop() { stackTransfer(pushStack, popStack); if (popStack.isEmpty()) thrownewRuntimeException("Queue is empty"); return popStack.pop(); } /** Get the front element. */ publicintpeek() { stackTransfer(pushStack, popStack); if (popStack.isEmpty()) thrownewRuntimeException("Queue is empty"); return popStack.peek(); } /** Returns whether the queue is empty. */ publicbooleanempty() { return pushStack.isEmpty() && popStack.isEmpty(); }
/** Put elements from stack1 into stack2. */ publicvoidstackTransfer(Stack stack1, Stack stack2) { if (stack2.isEmpty()) { while (!stack1.isEmpty()) { stack2.push(stack1.pop()); } } } }
def__init__(self): """ Initialize your data structure here. """ self.push_stack = [] self.pop_stack = []
defpush(self, x): """ Push element x to the back of queue. """ self.stack_transfer(self.pop_stack, self.push_stack) self.push_stack.append(x)
defpop(self): """ Removes the element from in front of queue and returns that element. """ self.stack_transfer(self.push_stack, self.pop_stack) iflen(self.pop_stack) == 0: returnNone return self.pop_stack.pop()
defpeek(self): """ Get the front element. """ self.stack_transfer(self.push_stack, self.pop_stack) iflen(self.pop_stack) == 0: returnNone return self.pop_stack[-1]
defempty(self): """ Returns whether the queue is empty. """ returnlen(self.push_stack) == 0andlen(self.pop_stack) == 0
defstack_transfer(self, stack1, stack2): """ Put elements from stack1 into stack2. """ iflen(stack2) == 0: whilelen(stack1) != 0: stack2.append(stack1.pop())
/** * Initialize your data structure here. */ publicMyStack() { queue = newLinkedList<>(); help = newLinkedList<>(); }
/** * Push element x onto stack. */ publicvoidpush(int x) { queue.add(x); }
/** * Removes the element on top of the stack and returns that element. */ publicintpop() { if (queue.isEmpty()) { thrownewRuntimeException("Empty stack"); } while (queue.size() != 1) { help.add(queue.poll()); }
intresult= queue.poll(); swap(); return result; }
/** * Get the top element. */ publicinttop() { if (queue.isEmpty()) { thrownewRuntimeException("Empty stack"); } while (queue.size() != 1) { help.add(queue.poll()); }