您的位置:首页 > 其它

【Leetcode】171. Excel Sheet Column Number

2017-01-20 14:06 246 查看
方法一:非递归

public class Solution {
public int titleToNumber(String s) {
int result = 0, len = s.length();
for (int i = 0; i < len; i++)
result = result * 26 + s.charAt(i) - 'A' + 1;
return result;
}
}


Runtime:2ms

方法二:递归

public class Solution {
public int titleToNumber(String s) {
int result = 0, len = s.length();
if (len == 1)
return s.charAt(0) - 'A' + 1;
return titleToNumber(s.substring(0, len - 1)) * 26 + s.charAt(len - 1) - 'A' + 1;
}
}
Runtime:2ms
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: