-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexcel-sheet-column-number.rs
41 lines (39 loc) · 1.22 KB
/
excel-sheet-column-number.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// 171. Excel Sheet Column Number
// 🟢 Easy
//
// https://leetcode.com/problems/excel-sheet-column-number/
//
// Tags: Math - String
struct Solution;
impl Solution {
// Convert from base 26 to base 10, pop characters from the end of the
// input and convert them to the equivalent base 10 value adding it to
// the result.
//
// Time complexity: O(n) - Where n is the number of characters in the
// input.
// Space complexity: O(n) - Where n is the number of characters in the
// input.
//
// Runtime 0 ms Beats 100%
// Memory 2 MB Beats 96.34%
pub fn title_to_number(column_title: String) -> i32 {
// If the exponents of 26 were bigger, we could precompute and store
// them in an array but they are only accessed once and go to 26^7 max.
column_title
.chars()
.rev()
.enumerate()
.fold(0, |cur, (i, c)| {
cur + (c as usize - 64) * 26_usize.pow(i as u32)
}) as i32
}
}
// Tests.
fn main() {
let tests = [("A", 1), ("AB", 28), ("ZY", 701)];
for test in tests {
assert_eq!(Solution::title_to_number(String::from(test.0)), test.1);
}
println!("All tests passed!")
}