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