Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 09f4055

Browse files
authored
Improved task 1837
1 parent 709a68c commit 09f4055

File tree

1 file changed

+8
-4
lines changed
  • src/main/java/g1801_1900/s1837_sum_of_digits_in_base_k

1 file changed

+8
-4
lines changed

‎src/main/java/g1801_1900/s1837_sum_of_digits_in_base_k/Solution.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package g1801_1900.s1837_sum_of_digits_in_base_k;
22

3-
// #Easy #Math #2022_05_07_Time_1_ms_(10.42%)_Space_38.9_MB_(91.55%)
3+
// #Easy #Math #2025_02_23_Time_0_ms_(100.00%)_Space_40.80_MB_(21.87%)
44

55
public class Solution {
66
public int sumBase(int n, int k) {
7-
Stringstr = Integer.toString(Integer.parseInt(n + "", 10), k);
7+
inta = 0;
88
int sum = 0;
9-
for (char c : str.toCharArray()) {
10-
sum += Character.getNumericValue(c);
9+
int b = 0;
10+
while (n != 0) {
11+
a = n % k;
12+
b = n / k;
13+
sum += a;
14+
n = b;
1115
}
1216
return sum;
1317
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /