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
danzzzz
V2EX  ›  Python

很难理解这道<<剑指 offer>>里面的题的这一段代码,想请教一下大家。

  •  1
     
  •   danzzzz · 2019-03-17 19:52:11 +08:00 · 3132 次点击
    这是一个创建于 1839 天前的主题,其中的信息可能已经有所发展或是发生改变。
    # 打印从 1 到最大的 n 位数
    
    class Solution:
        def Increment(self, number):
            overFlow = False
            carry = 0
            length = len(number)	
            for i in range(length - 1, -1, -1):
                num = ord(number[i]) - ord('0') + carry
                if i == length - 1:           
                    num += 1
                if num >= 10:
                    if i == 0:
                        overFlow = True
                    else:
                        num -= 10
                        carry = 1
                        number[i] = chr(ord('0') + num)
                else:
                    number[i] = chr(ord('0') + num)  
                    break
            return overFlow
    
        def PrintNum(self, number):
            string = ''.join(number)
            for i in range(len(string)):
                if string[i] != '0':
                    print(string[i:])
                    break
    
        def print1ToMaxOfNDigits(self, n):
            if n >= 0:
                number = ['0'] * n
                while not self.Increment(number):
                    self.PrintNum(number)
    
    

    主要是 Icreament 中的 for 循环小弟看不太懂,不懂的地方在于如何满十的时候进一个位,我比较笨,有时候绕不过来了,希望各位懂的大哥们帮帮忙,如果能得到对 Icreament 这段代码的说明就真的非常感谢了。

    9 条回复    2019-03-18 08:59:16 +08:00
    ipoh
        1
    ipoh  
       2019-03-17 20:45:20 +08:00
    carry = 1 表示进位+1
    jmc891205
        2
    jmc891205  
       2019-03-17 20:51:56 +08:00
    num 大于 10 就表示该进位了 把 carry 设成 1
    Qzier
        3
    Qzier  
       2019-03-17 21:35:51 +08:00 via iPhone
    代码浓浓的 Java 风
    darkTianTian
        4
    darkTianTian  
       2019-03-17 21:46:44 +08:00
    呃,那个,Python int 没有长度限制,个人认为,如果是 python 相关岗位的话,不应该考这道题。
    darkTianTian
        5
    darkTianTian  
       2019-03-17 21:56:41 +08:00
    https://github.com/darkTianTian/sword-for-offer 我最近也在刷第二遍,有些答案可以参考。
    geelaw
        6
    geelaw  
       2019-03-17 21:59:05 +08:00
    这个代码是画蛇添足,根本不需要自己模拟进位。

    class PartialSolution:
    def __init__(self, n):
    self.list = [''] * n
    def enumInts(self, current, begin):
    if current == len(self.list):
    print(''.join(self.list))
    else:
    for i in range(begin, 10):
    self.list[current] = str(i)
    self.enumInts(current + 1, 0)

    class Solution:
    def solve(self, n):
    if n >= 1:
    PartialSolution(1).enumInts(0, 0)
    if n >= 2:
    for i in range(2, n + 1):
    PartialSolution(i).enumInts(0, 1)

    Solution().solve(0)
    print('----')
    Solution().solve(1)
    print('----')
    Solution().solve(2)

    如果缩进被吃了你可以自己通过逻辑来还原正确的缩进
    danzzzz
        7
    danzzzz  
    OP
       2019-03-17 23:04:22 +08:00
    @darkTianTian 非常感谢。
    xern
        8
    xern  
       2019-03-18 01:48:59 +08:00 via Android
    书上这个题的代码写得不好,建议网上查一下,有讲得更好的。
    TheWalkingDead
        9
    TheWalkingDead  
       2019-03-18 08:59:16 +08:00
    python 用驼峰看着很不习惯~~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2730 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 05:47 · PVG 13:47 · LAX 22:47 · JFK 01:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.