【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 ...
随机推荐
- STM32407+LAN8720A+LWIP 实现TCP Client
硬件 一.配置CubeMax工程 二.配置系统时钟 因为LAN8720使用的是外部25MHz的晶振,所以不需要单片机输出时钟 三.配置ETH和LWIP参数 四.更改代码 LAN8720A在初始化的时候 ...
- 利用cocoapods管理开源项目,支持 pod install安装整个流程记录(github公有库)
利用cocoapods管理开源项目,支持 pod install安装整个流程记录(github公有库),完成预期的任务,大致有下面几步: 1.代码提交到github平台 2.创建.podspec 3. ...
- HashMap 与 Hashtable 的区别
Hashtable t 小写 二者用法一致 都实现Map接口 1.HashMap 的键值可以为null,而Hashtable不允许("null" 不是 null 前者是字符串 ...
- Python的各种推导式合集
推导式的套路 之前我们已经学习了最简单的列表推导式和生成器表达式.但是除此之外,其实还有字典推导式.集合推导式等等. 下面是一个以列表推导式为例的推导式详细格式,同样适用于其他推导式. variabl ...
- 阿里云对象存储OSS与文件存储NAS的区别
一.简介 应用场景:选择一款存储产品,面向文档数据的存取,不会涉及到数据处理. 产品选型主要从OSS和NAS中选择一款,满足文档存储的需求. 二.NAS优缺点 NAS 是一种采用直接与网络介质相连的特 ...
- 数据标记系列——标记工具Imagtagger
https://github.com/bit-bots/imagetagger 待有空说一说!
- eclipse(STS)安装jd-eclipse插件实现查看API源代码功能
emmm,IDEA确实是比STS智能很多,不过适当的转化也是需要的,这里介绍一下eclipse(STS)实现查看class反编译的源文件的功能 去Java Decompiler官网下一下eclipse ...
- webBrowser兼容
using Microsoft.Win32; using System; using System.Collections.Generic; using System.ComponentModel; ...
- jsp篇 之 基本概念
Jsp概念: 1.jsp是什么 jsp全称Java Server Pages,是一种[动态网页开发技术]. .html文件是静态页面 .jsp 文件是动态页面 jsp页面允许我们在html代码中[嵌入 ...
- Activiti工作流框架——快速上手
一.前言 最近在做公司的OA,里面有用到工作流,公司用的是 jbpm4,感觉比较老,资料有点少,就先学学 新一点的 activiti ㄟ(▔▽▔)ㄏ 首先工作流的概念是:工作流(Workfl ...