当前位置: 代码迷 >> 综合 >> [LeetCode 171]Excel Sheet Column Number
  详细解决方案

[LeetCode 171]Excel Sheet Column Number

热度:40   发布时间:2024-01-04 07:38:11.0

Related to question Excel Sheet Column Title

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

    A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 



solution:

26 进制,逐位*26相加

public int titleToNumber(String s) {if(s == null ||s.isEmpty()) return 0;if(s.length()==1) return s.charAt(0)-'A'+1;int result = s.charAt(0)-'A'+1;for(int i=1;i<s.length();i++){int cur = s.charAt(i)-'A'+1;result = result*26 + cur;}return result;}


  相关解决方案