Skip to content

Commit a744f2b

Browse files
committed
New Problem Solution - "1837. Sum of Digits in Base K"
1 parent 37209a7 commit a744f2b

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ LeetCode
1212
|1840|[Maximum Building Height](https://leetcode.com/problems/maximum-building-height/) | [C++](./algorithms/cpp/maximumBuildingHeight/MaximumBuildingHeight.cpp)|Hard|
1313
|1839|[Longest Substring Of All Vowels in Order](https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/) | [C++](./algorithms/cpp/longestSubstringOfAllVowelsInOrder/LongestSubstringOfAllVowelsInOrder.cpp)|Medium|
1414
|1838|[Frequency of the Most Frequent Element](https://leetcode.com/problems/frequency-of-the-most-frequent-element/) | [C++](./algorithms/cpp/frequencyOfTheMostFrequentElement/FrequencyOfTheMostFrequentElement.cpp)|Medium|
15+
|1837|[Sum of Digits in Base K](https://leetcode.com/problems/sum-of-digits-in-base-k/) | [C++](./algorithms/cpp/sumOfDigitsInBaseK/SumOfDigitsInBaseK.cpp)|Easy|
1516
|1835|[Find XOR Sum of All Pairs Bitwise AND](https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/) | [C++](./algorithms/cpp/findXorSumOfAllPairsBitwiseAnd/FindXorSumOfAllPairsBitwiseAnd.cpp)|Hard|
1617
|1834|[Single-Threaded CPU](https://leetcode.com/problems/single-threaded-cpu/) | [C++](./algorithms/cpp/singleThreadedCpu/SingleThreadedCpu.cpp)|Medium|
1718
|1833|[Maximum Ice Cream Bars](https://leetcode.com/problems/maximum-ice-cream-bars/) | [C++](./algorithms/cpp/maximumIceCreamBars/MaximumIceCreamBars.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Source : https://leetcode.com/problems/sum-of-digits-in-base-k/
2+
// Author : Hao Chen
3+
// Date : 2021-04-25
4+
5+
/*****************************************************************************************************
6+
*
7+
* Given an integer n (in base 10) and a base k, return the sum of the digits of n after converting n
8+
* from base 10 to base k.
9+
*
10+
* After converting, each digit should be interpreted as a base 10 number, and the sum should be
11+
* returned in base 10.
12+
*
13+
* Example 1:
14+
*
15+
* Input: n = 34, k = 6
16+
* Output: 9
17+
* Explanation: 34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.
18+
*
19+
* Example 2:
20+
*
21+
* Input: n = 10, k = 10
22+
* Output: 1
23+
* Explanation: n is already in base 10. 1 + 0 = 1.
24+
*
25+
* Constraints:
26+
*
27+
* 1 <= n <= 100
28+
* 2 <= k <= 10
29+
******************************************************************************************************/
30+
31+
class Solution {
32+
public:
33+
int sumBase(int n, int k) {
34+
int sum = 0;
35+
while(n > 0) {
36+
sum += (n % k);
37+
n /= k;
38+
}
39+
return sum;
40+
}
41+
};

0 commit comments

Comments
 (0)