1. Description
Related to question Excel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.
2. Runtime Distribution
3. Submission Details
4. Example
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
5. Code
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="Java" active="active"]
public int titleToNumber(String s) { if (s == null || s.length() == 0) { return 0; } int result = 0; int times = 1; for (int i = s.length() - 1; i >= 0; i--) { result += (s.charAt(i) + 1 - 'A') * times; times *= 26; } return result; }
[/restab]
[/restabs]
6.Test
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="Java" active="active" ]
public class LeetCode0171 { public int titleToNumber(String s) { if (s == null || s.length() == 0) { return 0; } int result = 0; int times = 1; for (int i = s.length() - 1; i >= 0; i--) { result += (s.charAt(i) + 1 - 'A') * times; times *= 26; } return result; } public static void main(String[] args) { LeetCode0171 leetcode = new LeetCode0171(); System.out.println(leetcode.titleToNumber("AC")); } }
[/restab]
[/restabs]
Comments | NOTHING