171. Excel Sheet Column Number
2016-05-14 22:05
295 查看
171. Excel Sheet Column Number
Description:Given a column title as appear in an Excel sheet, return its corresponding column number.
Example:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28
Link:
https://leetcode.com/problems/excel-sheet-column-number/
Analysis:
1.要弄清题目的意思,打开个EXCEL表格看一下就了然了。
2.我的理解是这只是进制之间的一种转换关系,AA 等这些数给人的感觉类似于26进制数。针对于本题就是26进制转10进制了。例如:A=1, AA=1*26+1, BA=2*26+1 。详见代码。
Source Code(C++):
#include <iostream> #include <string> #include <cmath> using namespace std; class Solution { public: int titleToNumber(string s) { int num=0; for (int i=s.length()-1; i>=0; i--) { int times = pow((float)26,(int)s.length()-1-i); num += (s.at(i)-64)*times; } return num; } }; int main() { Solution sol; cout << sol.titleToNumber("AA"); return 0; }
相关文章推荐
- bind出现Address already in use的原因及解决方法
- 函数
- http请求过程
- springmvc数据格式化
- Light OJ 1085 - All Possible Increasing Subsequences
- 搬家啦
- 编辑器面板(JEditorPane)
- 一样的“Too many open files”,不一样的阿里云和青云
- 取证分析标准作业程序???
- Spring对jdbc的支持。
- JLOI2016 方
- LogN的素数判断法和筛选法求素数的比较
- Twisted源码分析3
- TP框架基础 (二) ---空控制器和空操作
- 基于框架的博客Bloger系统开发的质量属性
- 100. Same Tree
- JDK源码解析之List和Set接口
- 机器学习常见面试题
- 中国餐馆过程(Chinese Restaurant Process)
- android:使用Properties保存程序配置