Skip to content

Commit 1b4df1c

Browse files
committed
1837. Sum of Digits in Base K
1 parent 21ce446 commit 1b4df1c

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
1. 아이디어:
3+
- 10진수로 이뤄진 n을 k진수로 변환 후, 각 자리수의 합 구하기
4+
- while문으로 직접 자릿수 계산
5+
6+
2. 시간복잡도: O(logN)
7+
8+
3. 자료구조/알고리즘: 진법 변환 원리, 시뮬레이션
9+
*/
10+
11+
class Solution {
12+
public int sumBase(int n, int k) {
13+
14+
int ans = 0;
15+
16+
while(n>0) {
17+
int p = n%k;
18+
19+
ans += p;
20+
n/=k;
21+
}
22+
23+
return ans;
24+
}
25+
}

0 commit comments

Comments
 (0)