本文主要是介绍[LeetCode 171]Excel Sheet Column Number,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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;}
这篇关于[LeetCode 171]Excel Sheet Column Number的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!