【leetcode】Excel Sheet Column Title & Excel Sheet Column Number (easy)

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 

思路:

相当于10进制转26进制。与一般不一样的是10进制对应的是0 - 9。而这个26进制对应的是 A(1)- Z(26), 没有0。

我的代码:

string convertToTitle(int n) {
        string ans;
        while(n != 0)
        {
            int num = n % 26;
            n /= 26;
            if(num != 0)
            {
                ans.push_back(num - 1 + ‘A‘);
            }
            else
            {
                ans.push_back(‘Z‘);
                n--;
            }
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }

大神精简的代码:

string convertToTitle(int n) {
    string res;
    char tmp;
    while(n){
        n -= 1; //这里相当于把A-Z表示成了0-25就与一般的表达一样了
        tmp = ‘A‘ + n % 26;
        res = tmp + res;
        n /= 26;
    }
    return res;
}

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进制转10进制。 以AAA为例   AAA = A * 26+ A * 261 + A * 260;

int titleToNumber(string s) {
        int ans = 0;
        int factor = 1;
        while(!s.empty())
        {
            ans = ans + (s.back() - ‘A‘ + 1) * factor;
            s.pop_back();
            factor *= 26;
        }
        return ans;
    }

大神精简版的:

int result = 0;
for (int i = 0; i < s.size(); result = result * 26 + (s.at(i) - ‘A‘ + 1), i++);
return result;
时间: 2024-08-28 11:04:51

【leetcode】Excel Sheet Column Title & Excel Sheet Column Number (easy)的相关文章

【leetcode】Remove Nth Node From End of List(easy)

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. 思路: 最基本的思路肯定

【leetcode】Best Time to Buy and Sell 2(too easy)

Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). Ho

【Leetcode】17、Letter Combinations of a Phone Number

题目 Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letter

【LeetCode】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 思路: 其实就是个进制转换.水水就过.倒是 Python 的代码让我

【LeetCode】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 Solution:26进制转十进制,AAA=27*26+1 1 class

【LeetCode】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 简单题,相当于26进制 class Solution { publi

【PHP】PHP使用PHPExcel生成Excel表格文件(附带随机生成英文名函数)

[PHP]PHP使用PHPExcel生成Excel表格文件(附带随机生成英文名函数) 前言 由于业务需要,我们需要从业务中汇总数据,并生成Excel文件. 思路是这样的 PHP要导出Excel表格文件->找一个好用的第三方库吧->在Composer的Packages里找一个吧->PHPExcel这么多收藏,就它了! PHPExcel 概述 PHPExcel is a library written in pure PHP and providing a set of classes th

【图解】Web前端实现类似Excel的电子表格

在本文中,我将用图解的方式用Wijmo(JavaScript库)中的SpreadJS来一步一步实现网页上的电子表格产品SpreadSheet(例如可构建Office 365 Excel产品.Google的在线SpreadSheet). 博文简介: Wijmo控件,是葡萄城提供的HTML\JavaScript库,目前最新版是2014 V2(2014.7.22号发布),支持jQuery UI和jQuery.jQuery Mobile.Angular.js.Bootstrap.js.Knockout.

【标题】一本帮你提高Excel办公效率的VBA书

公司工程部男同事,EXCEL能力最强的前三位,分别号称:大表哥 二表哥 三表哥 公司财务部女同事,EXCEL能力最强的前三位,分别号称:大表姐 二表姐 三表姐 想成为你们公司的“表哥”.“表姐”? 但是不会VBA. 那么, 我来了! 清华大学出版社推出的<Excel 2013 VBA入门与应用> 零基础 从无到有 循序渐进 200余VBA源文件 300分钟教学视频 清华大学出版社奉献 是写给Excel制表人员看的VBA书,简单.通俗. 是写给财务人员看的VBA书,上手快.入门快. 购买链接:

【LeetCode】Algorithms 题集(三)

Search Insert Position 意: Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,