【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的更多相关文章

  1. LeetCode Day4——Factorial Trailing Zeroes

    /* * Problem 172: Factorial Trailing Zeroes * Given an integer n, return the number of trailing zero ...

  2. 【算法训练营day4】LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表II

    [算法训练营day4]LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表 ...

  3. 【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 ...

  4. leetcode每日刷题计划-简单篇day4

    腰酸腿疼肝数模 被教育说代码风格像是小学生而且有点冗余 QAQ之前面试官好像也说过orz努力改努力改 今天把前两天跳过的vector给简单看了一下补上了 Num 14 最长公共前缀 Longest C ...

  5. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  6. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  7. [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 ...

  8. 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 ...

  9. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

随机推荐

  1. ZooKeeper应用理论及其应用场景

    ZooKeeper Client APIZooKeeper Client Library提供了丰富直观的API供用户程序使用,下面是一些常用的API: ● create(path, data, fla ...

  2. laytpl.js 模板使用记录

    {{# for(var j = 0, len = d.length; j < len; j++){ }} <div class="pure-u-1-5 pure-u-sm-1 p ...

  3. Using Sphinx to index CNS database

    1, look at the sphinx.person.address.conf to see how to configure the conf file2, index the database ...

  4. System.Web.Optimization找不到引用怎么办?

    Install-Package Microsoft.AspNet.Web.Optimization

  5. nodejs实践-MongoDB

    nodejs实践-MongoDB laiqun@msn.cn Contents 1. 特点: 2. 开始使用 3. 使用Mongoose操作MongoDB 4. 在express中使用,组织数据库相关 ...

  6. Linux的环境变量

     一.Linux的变量种类 按变量的生存周期来划分,Linux变量可分为两类: 1.永久的:需要修改配置文件,变量永久生效. 2.临时的:使用export命令声明即可,变量在关闭shell时失效. 二 ...

  7. dubbo Forbid blacklist

    http://www.jameswxx.com/dubbo/%E8%A7%A3%E5%86%B3dubbo%E9%97%AE%E9%A2%98%EF%BC%9Aforbid-consumer/

  8. MJRefresh

    automaticallyChangeAlpha 下拉或上拉时,文字颜色逐渐加深

  9. 如何查看和更改mysql数据库文件存放位置

    mysql数据库的数据文件默认是存放在:C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.1\data ...

  10. 程序员 10Tips

    理解技术债务 技术债务就像信用卡一样,会有很高的利息率,时间越长,修复所花的代价就越大,程序员对这个要有深刻的认识.同时团队应该培养一种保证设计质量的文化,应当鼓励重构.同时也应当鼓励其它有关代码质量 ...