Day35

  1. Add Digits
    思路:不能用循環,一個數的各位數相加,直到相加的和不超過10
    or x or y 布爾"或" - 如果 x 是非 0,它返回 x 的值,否則它返回 y 的計算值。

【有循環版本】

class Solution(object):
    def addDigits(self, num):
        """
        :type num: int
        :rtype: int
        """
        while num >= 10:
            sum1 =0
            while num !=0:
                a = num%10
                sum1 += a
                num = num/10
            num = sum1
        return num

【O(1)版本】

return num % 9 or 9 if num else 0

  1. Detect Capital
    思路:ascii碼中,A-Z的值小于a-z中的值,所以可以用減法判斷。
class Solution(object):
    def detectCapitalUse(self, word):
        """
        :type word: str
        :rtype: bool
        """
        return word.isupper() or word.islower() or word.istitle()
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容