1-line Python Solution with Example


  • 1
    M
    class Solution(object):
        def plusOne(self, digits):
            """
            :type digits: List[int]
            :rtype: List[int]
            """
    
            """
            >>> digits = [1, 2, 3]
            >>> map(str, digits)
            ['1', '2', '3']
            >>> ''.join(map(str, digits))
            '123'
            >>> int(''.join(map(str, digits)), 10)
            123
            >>> int(''.join(map(str, digits)), 10) + 1
            124
            >>> str(int(''.join(map(str, digits)), 10) + 1)
            '124'
            >>> map(int, str(int(''.join(map(str, digits)), 10) + 1))
            [1, 2, 4]
            """
            return map(int, str(int(''.join(map(str, digits)), 10) + 1))

  • 0
    class Solution(object):
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        s=''
        for x in  digits:
        	s+=str(x)
        return [ int(x) for x in list(str(int(s)+1)) ]
    

    跟我想的一样,序列和字符串的转化,可以直接带来结果,我要好好看看map


  • 0

    map(int,str(int(''.join(map(str,a)))+1))
    这样也足够了


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.