1
Fork 0
leetcode/source/excel_sheet_column_title/mod.rs

19 lines
503 B
Rust
Raw Normal View History

2022-04-09 11:04:21 +00:00
pub fn convert_to_title(column: i32) -> String {
let radix = 26;
let mut column = column as u32;
let mut result = vec![];
while column != 0 {
// Subtract 1 so the number 0 would equal the value 1.
let value = (column - 1) % radix;
column = (column - 1) / radix;
// Add 65 so the modulo'd value will be in the 65 through 90 character range
// (upppercase letters).
result.push(char::from_u32(value + 65).unwrap());
}
result.into_iter().rev().collect::<String>()
}