1
Fork 0

Solve excel-sheet-column-number.

This commit is contained in:
Bauke 2022-04-09 10:47:39 +02:00
parent 51fc3a4ffb
commit 50e994b2dc
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
3 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,18 @@
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
}

View File

@ -1,4 +1,5 @@
pub mod add_binary; pub mod add_binary;
pub mod excel_sheet_column_number;
pub mod implement_strstr; pub mod implement_strstr;
pub mod length_of_last_word; pub mod length_of_last_word;
pub mod longest_common_prefix; pub mod longest_common_prefix;

View File

@ -0,0 +1,11 @@
use leetcode::excel_sheet_column_number::title_to_number;
use test_case::test_case;
#[test_case("A", 1; "minimum")]
#[test_case("FXSHRXW", i32::MAX; "maximum")]
#[test_case("AB", 28; "double")]
#[test_case("AZY", 1377; "triple")]
fn test_excel_sheet_column_number(input: &str, expected: i32) {
assert_eq!(title_to_number(input.to_string()), expected);
}