We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 7a492f4 commit b09ba49Copy full SHA for b09ba49
src/0501-0600/504 - Base 7/base_7.rs
@@ -0,0 +1,23 @@
1
+impl Solution {
2
+ pub fn convert_to_base7(num: i32) -> String {
3
+ if num == 0 {
4
+ return "0".to_string();
5
+ }
6
+
7
+ let mut result = String::default();
8
+ let is_negative = num < 0;
9
+ let mut num = num.abs();
10
11
+ while num > 0 {
12
+ let rem = num % 7;
13
+ result.push_str(&rem.to_string());
14
+ num /= 7;
15
16
17
+ if is_negative {
18
+ result.push('-');
19
20
21
+ result.chars().rev().collect()
22
23
+}
0 commit comments