|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | + -* Excel Sheet Column Number *- |
| 5 | +
|
| 6 | +
|
| 7 | +
|
| 8 | + Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number. |
| 9 | +
|
| 10 | +For example: |
| 11 | +
|
| 12 | +A -> 1 |
| 13 | +B -> 2 |
| 14 | +C -> 3 |
| 15 | +... |
| 16 | +Z -> 26 |
| 17 | +AA -> 27 |
| 18 | +AB -> 28 |
| 19 | +... |
| 20 | +
|
| 21 | +
|
| 22 | +Example 1: |
| 23 | +
|
| 24 | +Input: columnTitle = "A" |
| 25 | +Output: 1 |
| 26 | +Example 2: |
| 27 | +
|
| 28 | +Input: columnTitle = "AB" |
| 29 | +Output: 28 |
| 30 | +Example 3: |
| 31 | +
|
| 32 | +Input: columnTitle = "ZY" |
| 33 | +Output: 701 |
| 34 | +
|
| 35 | +
|
| 36 | +Constraints: |
| 37 | +
|
| 38 | +1 <= columnTitle.length <= 7 |
| 39 | +columnTitle consists only of uppercase English letters. |
| 40 | +columnTitle is in the range ["A", "FXSHRXW"]. |
| 41 | +
|
| 42 | +*/ |
| 43 | + |
| 44 | +// extension Minus on String { |
| 45 | +// String operator -(String rhs) => replaceAll(rhs, ''); |
| 46 | +// } |
| 47 | + |
| 48 | +import 'dart:math'; |
| 49 | + |
| 50 | +class Solution { |
| 51 | +// Time Limit exceeds |
| 52 | + int titleToNumber(String columnTitle) { |
| 53 | + int sum = 0; |
| 54 | + for (String ch in columnTitle.split("")) { |
| 55 | + int diff = ch.codeUnitAt(0) - ('A'.codeUnitAt(0) - 1); |
| 56 | + sum = sum * 26 + diff; |
| 57 | + } |
| 58 | + return sum; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +class B { |
| 63 | +// Runtime: 656 ms, faster than 60.00% of Dart online submissions for Excel Sheet Column Number. |
| 64 | +// Memory Usage: 158.5 MB, less than 40.00% of Dart online submissions for Excel Sheet Column Number. |
| 65 | + |
| 66 | + int titleToNumber(String columnTitle) { |
| 67 | + // Initialize the output result... |
| 68 | + int result = 0; |
| 69 | + // Traverse the whole list through the loop... |
| 70 | + for (int i = 0; i < columnTitle.length; i++) { |
| 71 | + // Process to get the excel sheet column number... |
| 72 | + result *= 26; |
| 73 | + result += columnTitle[i].codeUnitAt(0) - 'A'.codeUnitAt(0) + 1; |
| 74 | + } |
| 75 | + return result; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +class C { |
| 80 | +// Runtime: 693 ms, faster than 40.00% of Dart online submissions for Excel Sheet Column Number. |
| 81 | +// Memory Usage: 148.3 MB, less than 60.00% of Dart online submissions for Excel Sheet Column Number. |
| 82 | + |
| 83 | + int titleToNumber(String columnTitle) { |
| 84 | + return columnTitle.length == 0 |
| 85 | + ? 0 |
| 86 | + : (columnTitle.codeUnitAt(columnTitle.length - 1) - |
| 87 | + 'A'.codeUnitAt(0) + |
| 88 | + 1) + |
| 89 | + 26 * |
| 90 | + titleToNumber(columnTitle.substring(0, columnTitle.length - 1)); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +class D { |
| 95 | + /* |
| 96 | + Idea: |
| 97 | +Similar to our decimal representation on base 10, the excel column title also has similar representation but with base 26. |
| 98 | +
|
| 99 | +In deciaml representation, for example, 123 would mean 1x10^2 + 2x10^1 + 3x10^0 |
| 100 | +In excel column title, for example, ABC would mean 1x26^2 + 2x26^1 + 3x26^0 |
| 101 | +
|
| 102 | +We can use charCodeAt to convert letter to number, but it is offset by 64, so we need to subtract 64. |
| 103 | +Then we can use the representation from above to find the sum |
| 104 | +
|
| 105 | + */ |
| 106 | + // Time lImit Exceed |
| 107 | + int titleToNumber(String columnTitle) { |
| 108 | + int sum = 0; |
| 109 | + for (int i = 0; i < columnTitle.length; i++) { |
| 110 | + int letter2Num = columnTitle.codeUnitAt(i) - 64; |
| 111 | + sum = (sum + pow(26, columnTitle.length - 1 - i) * letter2Num).toInt(); |
| 112 | + } |
| 113 | + |
| 114 | + return sum; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +class E { |
| 119 | +// Runtime: 726 ms, faster than 20.00% of Dart online submissions for Excel Sheet Column Number. |
| 120 | +// Memory Usage: 148.7 MB, less than 60.00% of Dart online submissions for Excel Sheet Column Number. |
| 121 | + int titleToNumber(String columnTitle) { |
| 122 | + int ans = 0; |
| 123 | + |
| 124 | + for (int i = 0; i < columnTitle.length; i++) { |
| 125 | + ans *= 26; |
| 126 | + int c = columnTitle.codeUnitAt(i); |
| 127 | + int n = (c - 'A'.codeUnitAt(0) + 1); |
| 128 | + ans += n; |
| 129 | + } |
| 130 | + return ans; |
| 131 | + } |
| 132 | +} |
0 commit comments