Post

leetcode(리트코드)-1837 Sum of Digits in Base K(python)

leetcode 1837 - Sum of Digits in Base K 문제입니다.

1. 문제

https://leetcode.com/problems/sum-of-digits-in-base-k/


2. Input , Output


3. 분류 및 난이도

Eazy 난이도 문제입니다.


4. 문제 해석

  • n이라는 숫자가 들어옵니다.
  • k진법으로 바꿨을 때 숫자의 각 자리를 더해서 리턴합니다.

5. code

코드설명

python

1
2
3
4
5
6
7
8
9
10
class Solution:
    def sumBase(self, n: int, k: int) -> int:
        res = ""
        while n :
            res += str(n % k)
            n = n // k
        sumres = 0 
        for i in range(len(res)):
            sumres += int(res[i])
        return sumres

6. 결과 및 후기, 개선점

필요시 c++로 짜드리겠습니다.

This post is licensed under CC BY 4.0 by the author.