Skip to content

Commit b09ba49

Browse files
committed
solved: 504. Base 7
Signed-off-by: rajput-hemant <[email protected]>
1 parent 7a492f4 commit b09ba49

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Diff for: src/0501-0600/504 - Base 7/base_7.rs

+23
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+
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

Comments
 (0)