Skip to content

Commit 79d264c

Browse files
committed
Sync LeetCode submission - Base 7 (rust)
1 parent 679cbb4 commit 79d264c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

my-folder/problems/base_7/solution.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn convert_to_base7(num: i32) -> String {
3+
let negative = num.is_negative();
4+
let mut num = num.abs() as u32;
5+
6+
let mut n = String::new();
7+
if num == 0 {
8+
return "0".into();
9+
}
10+
11+
while num != 0 {
12+
n.push(char::from_digit(num % 7, 10).unwrap());
13+
num /= 7;
14+
}
15+
16+
if negative {
17+
n.push('-');
18+
}
19+
20+
n.chars().rev().collect()
21+
}
22+
23+
}

0 commit comments

Comments
 (0)