1
Fork 0

Solve excel-sheet-column-title.

This commit is contained in:
Bauke 2022-04-09 13:04:21 +02:00
parent 50e994b2dc
commit 2c193f140d
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
3 changed files with 30 additions and 0 deletions

View File

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

View File

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

View File

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