Jade Dungeon

堆栈

反转操作

python

开新空间版。。。。

def reverse(src, new=None):
    if not new:
        new = []
    new.append(src.pop())
    if src:
        reverse(src, new)
    return new

s = [1, 2, 3, 5, 7, 10, 11, 12, 13, 2014, 25]
s = reverse(s)

黑科技版。。。。

def reverse(s):
    return s[::-1]

s = [1, 2, 3, 5, 7, 10, 11, 12, 13, 2014, 25]
s = reverse(s)

面试官背下来的版本(应该。。。)

def reverse(s):
  if len(s) == 0:
    return
  else:
    e = s.pop()
    reverse(s)
    pushStack(s, e)

def pushStack(s, e):
  if len(s) == 0:
    s.append(e)
    return
  else:
    a = s.pop()
    pushStack(s, e)
    s.append(a)

s = [1, 2, 3, 5, 7, 10, 11, 12, 13, 2014, 25]
s = reverse(s)