【leetcode】171. Excel Sheet Column Number
2016-06-05 11:23
429 查看
一、题目描述
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
c++代码(8ms)
思路:先找规律,找到规律后很容易就能写出代码
#include<iostream>
#include<string>
#include<math>
using namespace std;
class Solution {
public:
int titleToNumber(string s) {
int len=s.length();
int result = 0;
for(int i = 1; i<=len; i++)
result += pow(26, i-1) * (s[len-i] - 'A' + 1);
return result;
}
};
看了一下discuss,大神的代码如下:
java
int result = 0;
for (int i = 0; i < s.length(); result = result * 26 + (s.charAt(i) - 'A' + 1), i++);
return result;
c++
int result = 0;
for (int i = 0; i < s.size(); result = result * 26 + (s.at(i) - 'A' + 1), i++);
return result;
python
return reduce(lambda x, y : x * 26 + y, [ord(c) - 64 for c in list(s)])
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28
c++代码(8ms)
思路:先找规律,找到规律后很容易就能写出代码
#include<iostream>
#include<string>
#include<math>
using namespace std;
class Solution {
public:
int titleToNumber(string s) {
int len=s.length();
int result = 0;
for(int i = 1; i<=len; i++)
result += pow(26, i-1) * (s[len-i] - 'A' + 1);
return result;
}
};
看了一下discuss,大神的代码如下:
java
int result = 0;
for (int i = 0; i < s.length(); result = result * 26 + (s.charAt(i) - 'A' + 1), i++);
return result;
c++
int result = 0;
for (int i = 0; i < s.size(); result = result * 26 + (s.at(i) - 'A' + 1), i++);
return result;
python
return reduce(lambda x, y : x * 26 + y, [ord(c) - 64 for c in list(s)])
相关文章推荐
- 1688 求逆序对
- 【转】Android中的颜色设置
- 算分与数据结构 - 冒泡思想
- 【DAY15】Java第十五天I/O学习笔记
- 在QT的界面工程中添加一个简单的线程socket通信
- 栈帧
- 事件分发拦截机制
- Android 使用xml序列化器生成xml文件
- 提高zxing生成二维码的容错率及zxing生成二维码的边框设置
- Spring AOP 实现原理与 CGLIB 应用
- 提高工作效率
- virtualenv开发环境配置
- 好吧,这又是一个圆形进度条!
- 部门沟通规范
- Express Hello
- 基于Spring的包含特定注解bean的package扫描工具
- Json转换类库
- 第十二周项目1-实现复数类中的运算符重载(1)
- VLATTICE - Visible Lattice Points [Spoj 7001]
- Mybatis generator 自动生成代码时 ,insert如何返回主键值