23.04.26 Today’s Leetcode
258. Add Digits (easy)
class Solution:
def addDigits(self, num: int) -> int:
res = 0
if num < 10:
return num
while num > 0:
print(num, res)
res += num % 10
num = num // 10
return self.addDigits(res)
2160. Minimum Sum of Four Digit Number After Splitting Digits (easy)
class Solution:
def minimumSum(self, num: int) -> int:
digits = []
while num:
digits.append(num % 10)
num //= 10
digits.sort()
n = len(digits)
a = b = 0
for i in range(0, n, 2):
a *= 10
a += digits[i]
b *= 10
b += digits[i + 1]
return a + b
2544. Alternating Digit Sum (easy)
class Solution:
def alternateDigitSum(self, n: int) -> int:
t = str(n)
res = 0
sign = True
for i in range(len(t)):
res += int(t[i]) * (1 if sign else -1)
sign = not sign
return res
2553. Separate the Digits in an Array (easy)
class Solution:
def separateDigits(self, nums: List[int]) -> List[int]:
def help(n):
arr = []
while n:
arr.append(n % 10)
n //= 10
return arr[::-1]
res = []
for num in nums:
res.extend(help(num))
return res
2554. Maximum Number of Integers to Choose From a Range I (medium)
class Solution:
def maxCount(self, banned: List[int], n: int, maxSum: int) -> int:
res = 0
cur = 0
banned = set(banned)
for i in range(1, n + 1):
if i in banned:
continue
cur += i
if cur > maxSum:
break
res += 1
return res