欢迎光临散文网 会员登陆 & 注册

LeetCode 1837. Sum of Digits in Base K

2023-06-26 10:42 作者:您是打尖儿还是住店呢  | 我要投稿

Given an integer n (in base 10) and a base k, return the sum of the digits of n after converting n from base 10 to base k.

After converting, each digit should be interpreted as a base 10 number, and the sum should be returned in base 10.

 

Example 1:

Input: n = 34, k = 6Output: 9Explanation: 34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.

Example 2:

Input: n = 10, k = 10Output: 1Explanation: n is already in base 10. 1 + 0 = 1.

 

Constraints:

  • 1 <= n <= 100

  • 2 <= k <= 10

先按照k转化成k进制的数字,然后依次求每个数字之和即可;

下面是代码:

Runtime: 0 ms, faster than 100.00% of Java online submissions for Sum of Digits in Base K.

Memory Usage: 39.4 MB, less than 59.69% of Java online submissions for Sum of Digits in Base K.


LeetCode 1837. Sum of Digits in Base K的评论 (共 条)

分享到微博请遵守国家法律