leetcode day4
【171】Excel Sheet Column Number
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
思路: 实际上是做一个26进制的数字转换表,用一个数在for循环内纪录字符的位数,从而乘以基数26
public class Solution {
public int titleToNumber(String s) {
if(s==null||s.length()==0){
return 0;
}
int base = 0;
for(int i = 0; i<s.length();i++){
base = base*26 + s.charAt(i)-'A'+1;//当有两位数的时候,base就是个位数*26
}
return base;
}
}
逆问题:Excel Sheet Column Title【168】
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB 思路:给n,用while循环,取出n的每一位,然后对应变换
首先看取出一个数的各个位用Java来实现:
while(n!=0){
System.out.println("result is "+ n%10);
n = n/10;
}//输入1234,则输出4,3,2,1
所以这个也是,依次取出各个位,然后跟A比较后转换
public class Solution {
public String convertToTitle(int n) {
StringBuilder result = new StringBuilder();
while(n!=0){
result.insert(0, (char)('A' + (n-1) % 26));//因为是26进制,但是是从1开始计数,所以要减一
n = (n-1)/ 26;
}
return result.toString();
}
}
leetcode day4的更多相关文章
- LeetCode Day4——Factorial Trailing Zeroes
/* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...
- 【算法训练营day4】LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表II
[算法训练营day4]LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表 ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- leetcode每日刷题计划-简单篇day4
腰酸腿疼肝数模 被教育说代码风格像是小学生而且有点冗余 QAQ之前面试官好像也说过orz努力改努力改 今天把前两天跳过的vector给简单看了一下补上了 Num 14 最长公共前缀 Longest C ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
随机推荐
- ubuntu 12.04 安装snort acidbase相关注意事项
一.安装Snort 1.安装libpcap 1 apt-get install libpcap-dev 2.安装snort 1 2 apt-get install snort apt-get inst ...
- Linux 下 git的使用
参考链接:http://www.liaoxuefeng.com 安装 安装步骤: ①先给操作系统装入git工具,以Linux为例: $ sudo apt-get install git ②去githu ...
- Oracle数据库设计小细节
1. 如果使用PowerDesigner此类工具,注意将工具的导出的SQL语句中对于表的双引号去掉. 2. 建表和建字段的时候,不同单词之间使用下划线分隔,比如 REC_ID 3. Oracle中数值 ...
- Windows Server 2003 下如何安装及配置 FTP 服务器(转)
Windows Server 2003 下如何安装及配置 FTP 服务器 一.安装 FTP 服务器组件: 写在这里的一点 : 安装及配置 FTP 服务器之前 , 必须先手工配置服务器本身的 IP 地址 ...
- Css span div
SPAN元素和DIV元素有什么区别 解决思路: 最明显的区别是:DIV是块元素,SPAN是内嵌元素.块元素相当于内嵌元素在前后各加一个<br>换行.其实,块元素和行内元素也不是一成不变的, ...
- jsp的Get 与 SET的区别
getParameter:获取前个页面的数据,此方法获取的数据是从前台提交过来的 getAttribute:是获取setAttribute存储的数据 ========================= ...
- CG之菲涅尔效果简单实现
菲涅尔效果,指当光到达两种材质的接触面时,一些光在接触面的表面被反射出去,而另一部分光将发生折射穿过接触面. 现在要用shader来实现这种效果,如果要精确地描述这种底层的物理,其计算公式是非常复杂的 ...
- Android Studio的使用(四)--生成Get、Set方法
如何快速生成Get.Set方法,在我们编程中经常使用,下面将详细介绍. 1.右击代码编辑区域,并选择Generate. 2.在弹出框中选择Getter and Setter. 3.在弹出框中全选所有变 ...
- JAVA基础--常用类 String,StringBuffer, 基础数据类型包装类, Math类, Enum类
字符串相关类: String, StringBuffer String类为不可变的字符序列 String s1="hello"; String s2="hello&quo ...
- FIR滤波器的实现方法(转)
源:http://blog.sina.com.cn/s/blog_493520900102uy26.html 内容来自于上篇博文,第七章,FIR滤波器 http://blog.sina.com.cn/ ...