zoukankan      html  css  js  c++  java
  • 171. Excel Sheet Column Number

    题目:

    Related to question Excel Sheet Column Title

    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 

    链接: http://leetcode.com/problems/excel-sheet-column-number/

    题解:

    找到公式以后从前向后遍历一次。这种题目估计可能会出个数据结构之类的题结合excel column number一起考。

    Time Complexity - O(n), Space Complexity - O(1)。

    public class Solution {
        public int titleToNumber(String s) {
            if(s == null || s.length() == 0)
                return 0;
            int res = 0;
            
            for(int i = 0; i < s.length(); i++)
                res =  res * 26 + s.charAt(i) - 'A' + 1; 
            
            return res;
        }
    }

    二刷:

    跟一刷一样,发现公式就好。

    Java:

    Time Complexity - O(n), Space Complexity - O(1)。

    public class Solution {
        public int titleToNumber(String s) {
            if (s == null || s.length() == 0) {
                return 0;
            }
            int res = 0;
            for (int i = 0; i < s.length(); i++) {
                res = res * 26 + s.charAt(i) - 'A' + 1;
            }
            return res;
        }
    }

    三刷:

    头脑和思路要清晰,争取一点一点过每一道题,一定要熟练。

    Java:

    public class Solution {
        public int titleToNumber(String s) {
            if (s == null || s.length() == 0) {
                return 1;
            }
            int res = 0;
            for (int i = 0; i < s.length(); i++) {
                res = res * 26 + (s.charAt(i) - 'A') + 1;
            }
            return res;
        }
    }

    题外话: 这两天抓紧时间打通了Starcraft II: Legacy of the Void, 好累。 原定12月7号在波士顿的培训被取消了,改成了在线课程...擦...在线就没有伙食和路费的报销了擦..........好郁闷

    Reference:

  • 相关阅读:
    安卓笔记20170117
    android笔记20170116
    meta 标签的作用
    SASS 初学者入门
    JQuery selector
    浅谈js回调函数
    自己写的jquery 弹框插件
    魔兽种子
    html页面的CSS、DIV命名规则
    各种弹框素材的链接
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4491639.html
Copyright © 2011-2022 走看看