Solve excel-sheet-column-title.
This commit is contained in:
parent
50e994b2dc
commit
2c193f140d
|
@ -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>()
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
pub mod add_binary;
|
pub mod add_binary;
|
||||||
pub mod excel_sheet_column_number;
|
pub mod excel_sheet_column_number;
|
||||||
|
pub mod excel_sheet_column_title;
|
||||||
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;
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
Loading…
Reference in New Issue