【leetcode】Excel Sheet Column Title & Excel Sheet Column Number
2016-02-01 20:41
323 查看
两道相关联的题目,一个是将数字转换为26进制,一个是将26进制表示方法转换为10进制
第一次觉得java好慢。
第一次觉得java好慢。
public class Solution { public String convertToTitle(int n) { StringBuilder sb = new StringBuilder(); while (n > 0) { int apd = n % 26; char c; if (apd == 0) { c = 'Z'; n = n / 26 - 1; } else { c = (char) (apd + 'A' - 1); n = n / 26; } sb.append(c); } return sb.reverse().toString(); } }
public class Solution { public int titleToNumber(String s) { HashMap<Character, Integer> map = new HashMap<Character, Integer>(); char c = 'A'; for(int i=1;i<=26;++i){ map.put(c,i); c += 1; } int result = 0; int i = s.length()-1; int t = 0; while(i >= 0){ char curr = s.charAt(i); result = result + (int) Math.pow(26, t) * map.get(curr); t++; i--; } return result; } }
Math.pow(底数,几次方) 如:double a=2.0; double b=3.0; double c=Math.pow(a,b); 就是2的三次方是多少; c最终为8;
相关文章推荐
- uva673-栈-(字符读取和清空缓冲区)
- LaTex希腊字母
- Windows如何打包Qt程序
- codeforces 621A Wet Shark and Odd and Even
- Behavior的demo——粗仿天猫详情页效果
- 高精度除法
- 美赛matlab编程小总结
- 【UVA】658 - It's not a Bug, it's a Feature!(隐式图 + 位运算)
- 【郑轻 oj】 - 1046: 奇数的乘积
- MySQL中常见字符串操作函数
- freemaker用法
- Struts2之文件上传
- ubuntu安装原生迅雷,让下载成为简单
- Unity问答第4期
- Windows server上rsync的安装和使用
- JavaScript 命名规则
- 机房水题欢乐赛 2016-02-01
- 13 gifGenerator
- Qt核心剖析: 寻找 QObject 的源代码
- MySQLdb的安装与使用