8000 Add SumOfDigitsInBaseK · dnshi/Leetcode@5bde91c · GitHub
[go: up one dir, main page]

Skip to content

Commit 5bde91c

Browse files
committed
Add SumOfDigitsInBaseK
1 parent 3ab1b0d commit 5bde91c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
| # | Title | Solution | Difficulty |
66
|---| ----- | -------- | ---------- |
7+
|1839|[Longest Substring Of All Vowels in Order](https://leetcode.com/problems/longest-substring-of-all-vowels-in-order)|[JavaScript](algorithms/LongestSubstringOfAllVowelsInOrder.js)|Medium|
78
|1452|[People Whose List of Favorite Companies Is Not a Subset of Another List](https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list)|[JavaScript](algorithms/PeopleWhoseListOfFavoriteCompaniesIsNotASubsetOfAnotherList.js)|Medium|
89
|1451|[Rearrange Words in a Sentence](https://leetcode.com/problems/rearrange-words-in-a-sentence)|[JavaScript](algorithms/RearrangeWordsInASentence.js)|Medium|
910
|1450|[Number of Students Doing Homework at a Given Time](https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time)|[JavaScript](algorithms/NumberOfStudentsDoingHomeworkAtAGivenTime.js)|Easy|
11+
|1344|[Angle Between Hands of a Clock](https://leetcode.com/problems/angle-between-hands-of-a-clock)|[JavaScript](algorithms/AngleBetweenHandsOfAClock.js)|Medium|
1012
|1071|[Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings)|[JavaScript](algorithms/GreatestCommonDivisorOfStrings.js)|Easy|
1113
|1047|[Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string)|[JavaScript](algorithms/RemoveAllAdjacentDuplicatesInString.js)|Easy|
1214
|1046|[Last Stone Weight](https://leetcode.com/problems/last-stone-weight)|[JavaScript](algorithms/LastStoneWeight.js)|Easy|
@@ -103,6 +105,7 @@
103105
|345|[Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string) | [JavaScript](./algorithms/ReverseVowelsOfAString.js)|Easy|
104106
|344|[Reverse String](https://leetcode.com/problems/reverse-string) | [JavaScript](./algorithms/ReverseString.js)|Easy|
105107
|342|[Power of Four](https://leetcode.com/problems/power-of-four) | [JavaScript](./algorithms/PowerOfFour.js)|Easy|
108+
|341|[Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator)|[JavaScript](algorithms/FlattenNestedListIterator.js)|Medium|
106109
|326|[Power of Three](https://leetcode.com/problems/power-of-three) | [JavaScript](./algorithms/PowerOfThree.js)|Easy|
107110
|322|[Coin Change](https://leetcode.com/problems/coin-change) | [JavaScript](./algorithms/CoinChange.js)|Medium|
108111
|319|[Bulb Switcher](https://leetcode.com/problems/bulb-switcher) | [JavaScript](./algorithms/BulbSwitcher.js)|Medium|

algorithms/SumOfDigitsInBaseK.js

Lines changed: 39 additions & 0 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Source : https://leetcode.com/problems/sum-of-digits-in-base-k
2+
// Author : Dean Shi
3+
// Date : 2021-05-01
4+
5+
/***************************************************************************************
6+
* Given an integer n (in base 10) and a base k, return the sum of the digits of n
7+
* after converting n from base 10 to base k.
8+
*
9+
* After converting, each digit should be interpreted as a base 10 number, and the sum
10+
* should be returned in base 10.
11+
*
12+
* Example 1:
13+
*
14+
* Input: n = 34, k = 6
15+
* Output: 9
16+
* Explanation: 34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.
17+
*
18+
* Example 2:
19+
*
20+
* Input: n = 10, k = 10
21+
* Output: 1
22+
* Explanation: n is already in base 10. 1 + 0 = 1.
23+
*
24+
* Constraints:
25+
*
26+
* 1 <= n <= 100
27+
* 2 <= k <= 10
28+
*
29+
*
30+
***************************************************************************************/
31+
32+
/**
33+
* @param {number} n
34+
* @param {number} k
35+
* @return {number}
36+
*/
37+
var sumBase = function(n, k) {
38+
return n.toString(k).split('').reduce((sum, n) => +n + sum, 0)
39+
};

0 commit comments

Comments
 (0)
0