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

19 lines
404 B
Rust
Raw Normal View History

2022-04-09 08:47:39 +00:00
pub fn title_to_number(title: String) -> i32 {
let mut result = 0;
for (index, character) in title.chars().rev().enumerate() {
// Convert the character to a number and subtract 9 so A will equal 1.
let number = character.to_digit(36).unwrap() - 9;
let n = if index == 0 {
number
} else {
26_u32.pow(index as u32) * number
};
result += n;
}
result as i32
}