leetcode 258. Members are added (python)

Given a non-negative integer num, on the respective bits of the digital addition is repeated until the result is a number.

Example:

Input: 38
Output: 2
Explanation: Members of the process of adding: 11 = 3 + 8, 1 + 1 = 2. Because 2 is a number, it returns 2.

class Solution:
    def addDigits(self, num: int) -> int:
        
        def hanshu(nums):
            sum = 0
            while(nums>0):
                ge = nums % 10
                sum += ge
                nums = int(nums / 10)
            return sum
        
        sum = hanshu(num)
        while(sum>=10):
            sum = hanshu(sum)
        return sum

 

Guess you like

Origin www.cnblogs.com/xiaotongtt/p/11318571.html