Skip to content

Commit 9fe8bb1

Browse files
solves base 7
1 parent 4391612 commit 9fe8bb1

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@
132132
| 492 | [Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ConstructTheRectangle.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/construct_the_rectangle.py) |
133133
| 496 | [Next Greater Element I](https://leetcode.com/problems/next-greater-element-i) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/NextGreaterElementI.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/next_greater_element_i.py) |
134134
| 500 | [Keyboard Row](https://leetcode.com/problems/keyboard-row) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/KeyBoardRow.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/keyboard_row.py) |
135-
| 501 | [Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/FindModeInBinarySearchTree) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/find_mode_in_binary_search_tree.py) |
136-
| 504 | [Base 7](https://leetcode.com/problems/base-7) | Easy | |
135+
| 501 | [Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/FindModeInBinarySearchTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/find_mode_in_binary_search_tree.py) |
136+
| 504 | [Base 7](https://leetcode.com/problems/base-7) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/Base7.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/base_7.py) |
137137
| 506 | [Relative Ranks](https://leetcode.com/problems/relative-ranks) | Easy | |
138138
| 507 | [Perfect Number](https://leetcode.com/problems/perfect-number) | Easy | |
139139
| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number) | Easy | |

Diff for: python/base_7.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def convertToBase7(self, num: int) -> str:
3+
if num == 0:
4+
return '0'
5+
is_negative = num < 0
6+
num = -num if is_negative else num
7+
result = ''
8+
while num > 0:
9+
result = str(num % 7) + result
10+
num //= 7
11+
return ('-' if is_negative else '') + result

Diff for: src/Base7.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class Base7 {
2+
public static String convertToBase7(int number) {
3+
if (number == 0) return "0";
4+
StringBuilder result = new StringBuilder();
5+
boolean isNegative = number < 0;
6+
if (isNegative) {
7+
number *= -1;
8+
}
9+
while (number > 0) {
10+
result.append(number % 7);
11+
number /= 7;
12+
}
13+
return result.append(isNegative ? "-" : "").reverse().toString();
14+
}
15+
16+
public static void main(String[] args) {
17+
System.out.println(convertToBase7(-7));
18+
}
19+
}

0 commit comments

Comments
 (0)