Solve excel-sheet-column-number.
This commit is contained in:
parent
51fc3a4ffb
commit
50e994b2dc
|
@ -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
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
pub mod add_binary;
|
||||
pub mod excel_sheet_column_number;
|
||||
pub mod implement_strstr;
|
||||
pub mod length_of_last_word;
|
||||
pub mod longest_common_prefix;
|
||||
|
|
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue