class Solution {
public:
string convertToTitle(int n) {
if (n == ) {
return "";
}
return convertToTitle((n - ) / ) + (char)((n - ) % + 'A');
}
};
    A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
答案写法:
class Solution {
public:
int titleToNumber(string s) { int sum = ;
int tmp = ;
for (int i = ; i < s.length(); ++i) {
tmp = s[i] - 'A' + ;
sum = * sum + tmp;
}
return sum;
  }
};
下面是自己写的:
class Solution {
public:
int titleToNumber(string s) {
int len = s.length();
int num = ;
if (len == )
num = s[]-'A'+;
else{
num += s[len-]-'A'+;
int cal = ;
for (int i=len-;i>=;i--){
num += (s[i]-'A'+)*cal;
cal *= ;
} }
return num;
}
};//从字符串到数字

【easy】168. Excel Sheet Column Title 171. Excel Sheet Column Number的更多相关文章

  1. leetcode 168. Excel Sheet Column Title 171 Excel Sheet Column Number

    题目 //像10进制一样进行 转换   只是要从0开始记录 class Solution { public: string convertToTitle(int n) { char a; string ...

  2. 28. Search a 2D Matrix 【easy】

    28. Search a 2D Matrix [easy] Write an efficient algorithm that searches for a value in an mx n matr ...

  3. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  4. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  5. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  6. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  7. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  8. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  9. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

随机推荐

  1. IDEA远程调试监控端口

    大家知道,线上环境定位问题不是那么简单的,如果有非常完善的日志以及监控系统是不必担心的,但是应对这些并不完善的场景下,IDEA提供了一种远程调试的功能,remote集成了可以远程调试的功能,只需要在你 ...

  2. vc图像合成

    本程序下载地址: 上一篇讲述了tiff格式图片拆分成多张图片, 这篇博客讲述如何把多张任意格式的图片合成为一张图片. 图像合成仍然需要借助Cximage图像库,合成函数为Mixfrom, 函数原型为: ...

  3. SpringCloud(2)服务消费者(rest+ribbon)

    1.准备工作 这一篇文章基于上一篇文章的工程.启动eureka-server 工程,端口为 8761.分别以端口 8762 和 8763 启动 service-hi 工程.访问 localhost:8 ...

  4. Mariadb第一章:介绍及安装--小白博客

    mariadb(第一章)   数据库介绍 1.什么是数据库? 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以 ...

  5. 对于for循环中使用let或var时,i的作用域范围的记录

    在for循环中使用let时,结果如下 for内部定义的i在循环结束后不会覆盖外部的i 在for循环中使用var,且不控制i的作用域时,结果如下 第一个for循环内部定义的i并不会创建,而是直接使用外部 ...

  6. PS制作水火相溶特效文字图片

    最终效果 一.新建一个1400*900像素的画布. 二.由上到下拉一个深灰到纯黑径向渐变. 三.输入字母S,并用ctrl+t拉到适合的大小,并且降低不透明度. 四.拖入水花素材(如果大家有水花笔刷的话 ...

  7. 跨域两种解决方案CORS以及JSONP

    一.CORS设置请求头 设置请求头实现跨域: //跨域的浏览器会让请求带Origin头,表明来自哪里的跨域请求 Origin: http://xxx.example //表明允许跨域访问 Access ...

  8. 8.docker的安全性

    在查看Docker安全性时,有四个主要方面需要考虑: 内核的内在安全性及其对命名空间和cgroup的支持; Docker守护进程本身的攻击面; 容器配置配置文件中的漏洞,默认情况下或用户自定义时. 内 ...

  9. CODEVS 3546 矩阵链乘法

    http://codevs.cn/problem/3546/ 题目 给定有n个要相乘的矩阵构成的序列(链)<A1,A2,A3,.......,An>,要计算乘积A1A2.....An.一组 ...

  10. stop()在animate中的用法

    stop 是jQuery中用于控制页面动画效果的方法.运行之后立刻结束当前页面上的动画效果.stop在新版jQuery中添加了2个参数:第一个参数的意思是是否清空动画序列,也就是stop的是当前元素的 ...