- Add Digits
思路:不能用循環(huán)吱晒,一個(gè)數(shù)的各位數(shù)相加蔼紧,直到相加的和不超過10
or x or y 布爾"或" - 如果 x 是非 0怠惶,它返回 x 的值回季,否則它返回 y 的計(jì)算值。
【有循環(huán)版本】
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
- 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()