-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
36 lines (30 loc) · 978 Bytes
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
fn main() {
assert_eq!(Solution::convert_to_title(701), "A".to_owned());
}
struct Solution {}
impl Solution {
// transfer into 26-radix number
pub fn convert_to_title(column_number: i32) -> String {
let mut res = vec![];
let mut num = column_number as usize;
while num != 0 {
let reminder = if num % 26 == 0 { 26 } else { num % 26 };
res.push(reminder as u8 + 'A' as u8 - 1);
// num must be a multiple of 26 now
num = (num - reminder) / 26;
}
res.reverse();
String::from_utf8(res).unwrap()
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::convert_to_title(1), "A".to_owned());
assert_eq!(Solution::convert_to_title(28), "AB".to_owned());
assert_eq!(Solution::convert_to_title(701), "ZY".to_owned());
assert_eq!(Solution::convert_to_title(2147483647), "FXSHRXW".to_owned());
}
}