Discuss / Python / 用迭代应该是代码量最少的,只有6行

用迭代应该是代码量最少的,只有6行

Topic source

def trim(s):

    if (not s) or (s[0]!=' ' and s[-1]!=' '): #字符串为空或者左右都无空字符

        return s

    if s[0]==' ':   #如果左侧空

        return trim(s[1:])

    if s[-1]==' ':   #如果右侧空

        return trim(s[:-1])

说错了,是递归,不是迭代


  • 1

Reply