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. 【区块链】【一】Hash 算法【转】

    问题导读1.哈希算法在区块链的作用是什么?2.什么是哈希算法?3.哈希算法是否可逆?4.比特币采用的是什么哈希算法? 作用在学习哈希算法前,我们需要知道哈希在区块链的作用哈希算法的作用如下:区块链通过 ...

  2. Java 显示读取properties 乱码解决方案

    项目开发时,在配置springmvc 校验错误提示信息时,配置到properties的中文,在前端取出时,显示为乱码,可以确定properties 配置文件已经被设为UTF-8编码,在springmv ...

  3. 【转】Spark实现行列转换pivot和unpivot

    背景 做过数据清洗ETL工作的都知道,行列转换是一个常见的数据整理需求.在不同的编程语言中有不同的实现方法,比如SQL中使用case+group,或者Power BI的M语言中用拖放组件实现.今天正好 ...

  4. tomcat知识(一)

    1.tomcat配置javaWeb项目常见错误: ①:端口占用 ②:未配置JAVA_HOME环境变量 2.tomcat修改端口号 tomcat安装路径下面找到conf文件夹,修改server.xml文 ...

  5. nginx(一)初识nginx

    什么是nginx?Nginx (engine x) 是一款轻量级的Web 服务器 .反向代理服务器及电子邮件(IMAP/POP3)代理服务器. Nginx应用场景(都很常用): 1:http服务器.N ...

  6. Linux 学习 (七) 挂载命令 & 用户登陆查看

    Linux达人养成计划 I 学习笔记 挂载命令 mount:查询系统中已经挂载的设备 mount -a:根据配置文件 /etc/fstab 的内容,自动挂载 mount [-t 文件系统] [-o 特 ...

  7. jsp篇 之 jsp中的注释

    Jsp中的注释: 第一种: <!-- html/xml中的注释方式 --> 特点: 1.用户在浏览器中右键查看源代码 [能] 看到这个注释. 2.在服务器端,这个jsp页面被翻译成的jav ...

  8. BZOJ5506 GXOI/GZOI2019旅行者(最短路)

    本以为是个二进制分组傻逼题https://www.cnblogs.com/Gloid/p/9545753.html,实际上有神仙的一个log做法https://www.cnblogs.com/asul ...

  9. js侧边菜单

    目标 实现一个侧边栏菜单,最多二级,可以收起展开.用于系统左侧的主菜单. 大多数系统都会有这样的菜单,用于导航功能,切换到不同的操作页面.在单页应用系统中,菜单一般是固定在左侧,分组节点上配图标,高亮 ...

  10. [Nmap] Regular script

    out of date: TCP:nmap.exe -sC -sS -sV -p 1-65535 -A -v -v -oX filename.xml ip UDP:nmap.exe -sC -sU - ...