V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
withoutconscious
V2EX  ›  Python

Python 作业,复制栈原来的元素到栈顶,只能用一个额外的栈,求思路

  •  
  •   withoutconscious · 2022-02-22 17:42:05 +08:00 · 2232 次点击
    这是一个创建于 794 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大佬们,这题要求复制栈元素,然后再塞到栈顶,只能用一个额外的栈和一个 integer 变量,咋做? 比如 : (Top) 1, 2, 3, 4, 5 (Bottom) ⇒ (Top) 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 (Bottom)

    Write a function to duplicate the elements of stack s on top the original elements, by using only one additional queue q (initially empty), and one integer variable m. def dup_sq(s, q):

    2 条回复    2022-02-22 17:48:48 +08:00
    Itoktsnhc
        1
    Itoktsnhc  
       2022-02-22 17:47:14 +08:00   ❤️ 1
    额外的队列吧
    withoutconscious
        2
    withoutconscious  
    OP
       2022-02-22 17:48:48 +08:00
    @Itoktsnhc 对哦,我没看仔细😂,那会做了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   978 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 20:11 · PVG 04:11 · LAX 13:11 · JFK 16:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.