【easy】168. Excel Sheet Column Title 171. Excel Sheet Column Number
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的更多相关文章
- leetcode 168. Excel Sheet Column Title 171 Excel Sheet Column Number
题目 //像10进制一样进行 转换 只是要从0开始记录 class Solution { public: string convertToTitle(int n) { char a; string ...
- 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 ...
- 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 ...
- 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 ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 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 ...
- 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 ...
随机推荐
- Bootstrap开发框架界面的调整处理
我在之前介绍了很多关于Boostrap的框架方面的文章,主要是介绍各种插件的使用居多,不过有时候觉得基于Metronic的Boostrap框架的界面效果不够紧凑,希望对它进行一定的调整,那么我们应该如 ...
- 基于C#的钉钉SDK开发(1)--对官方SDK的重构优化
在前段时间,接触一个很喜欢钉钉并且已在内部场景广泛使用钉钉进行工厂内部管理的客户,如钉钉考勤.日常审批.钉钉投影.钉钉门禁等等方面,才体会到原来钉钉已经已经在企业上可以用的很广泛的,因此回过头来学习研 ...
- Activiti6事件及监听器配置(学习笔记)
1.事件及监听器原理 当流程引擎启动的时候,我们定义的监听器,就已经注册在一个事件类型上面. 注册的方式有多种,它可以注册在所有的事件类型上面.也可以注册在指定的几个事件类型上面,这样引擎启动的时候就 ...
- 使用bat脚本永久激活Windows系统(摘抄)
使用bat脚本永久激活Windows系统 每次重装完系统后,右下角会提示系统未激活,无法进行一些个性化设置. 在这里我自己写了一个bat脚本用于激活Windows系统.(仅供学习) 文件下载: 链 ...
- Android Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conflict with dependency
错误内容: Error:Execution failed for task ':app:preDebugAndroidTestBuild'.> Conflict with dependency ...
- Android学习第九天
为什么需要内容提供者 a) 如何创建数据库 b) 文件权限 c) Chmod linux修改权限 内容提供者原理 a) 内容提供者把数据进行封 ...
- BZOJ4259残缺的字符串
题目描述 很久很久以前,在你刚刚学习字符串匹配的时候,有两个仅包含小写字母的字符串A和B,其中A串长度为m,B串长度为n.可当你现在再次碰到这两个串时,这两个串已经老化了,每个串都有不同程度的残缺. ...
- Django 中使用kindeditor
KindEditor 是一套开源的在线HTML编辑器,主要用于让用户在网站上获得所见即所得编辑效果,开发人员可以用 KindEditor 把传统的多行文本输入框(textarea)替换为可视化的富文本 ...
- ASP.NET概念
ASP.NET :是一个开发框架,用于通过 HTML.CSS.JavaScript 以及服务器脚本来构建网页和网站. ASP.NET两种开发语言:VB C#
- Django_restframework+vue解决跨域问题
1. 安装 pip3 install django-cors-headers 2.在settings.py里设置 INSTALLED_APPS = ( ... 'corsheaders', ... ) ...