5738:K 进制表示下的各位数字总和(优化)
This commit is contained in:
parent
749040b84c
commit
737d31db25
@ -43,11 +43,12 @@ public class SumOfDigitsInBaseK{
|
||||
class Solution {
|
||||
public int sumBase(int n, int k) {
|
||||
int sum = 0;
|
||||
String str = "";
|
||||
while (n > 0) {
|
||||
str += n % k;
|
||||
n /= k;
|
||||
}
|
||||
// StringBuilder str = new StringBuilder();
|
||||
// while (n > 0) {
|
||||
// str.append(n % k);
|
||||
// n /= k;
|
||||
// }
|
||||
String str = Integer.toString(n,k);
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
sum += str.charAt(i) - '0';
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user