C++实现LeetCode(171.求Excel表列序号)

[LeetCode] 171.Excel Sheet Column Number 求Excel表列序号

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 

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

这题实际上相当于一种二十六进制转十进制的问题,并不难,只要一位一位的转换即可。代码如下:

class Solution {
public:
    int titleToNumber(string s) {
        int n = s.size();
        int res = 0;
        int tmp = 1;
        for (int i = n; i >= 1; --i) {
            res += (s[i - 1] - 'A' + 1) * tmp;
            tmp *= 26;
        }
        return res;
    }
};

到此这篇关于C++实现LeetCode(171.求Excel表列序号)的文章就介绍到这了,更多相关C++实现求Excel表列序号内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

时间: 2021-08-01

C++实现LeetCode(166.分数转循环小数)

[LeetCode] 166.Fraction to Recurring Decimal 分数转循环小数 Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. Fo

C++实现LeetCode(228.总结区间)

[LeetCode] 228.Summary Ranges 总结区间 Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input:  [0,1,2,4,5,7] Output: ["0->2","4->5","7"] Explanation: 0,1,2 form a continuous ran

C++实现LeetCode(168.求Excel表列名称)

[LeetCode] 168.Excel Sheet Column Title 求Excel表列名称 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 ... Example 1: Input: 1 O

C++实现LeetCode(162.求数组的局部峰值)

[LeetCode] 162.Find Peak Element 求数组的局部峰值 A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. The array may contain multiple peaks, in that c

C++实现LeetCode(164.求最大间距)

[LeetCode] 164. Maximum Gap 求最大间距 Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Return 0 if the array contains less than 2 elements. Example 1: Input: [3,6,9,1] Output: 3 Explanation: The sor

C++实现LeetCode165.版本比较)

[LeetCode] 165.Compare Version Numbers 版本比较 Compare two version numbers version1 and version2. If version1 > version2 return 1; if version1 <version2 return -1;otherwise return 0. You may assume that the version strings are non-empty and contain onl

C++实现LeetCode(167.两数之和之二 - 输入数组有序)

[LeetCode] 167.Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序 Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of t

C++实现LeetCode(163.缺失区间)

[LeetCode] 163. Missing Ranges 缺失区间 Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges. Example: Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99, Output: ["2&q

Python实现输出某区间范围内全部素数的方法

本文实例讲述了Python实现输出某区间范围内全部素数的方法.分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- # 简述:区间范围101-200 # 要求:判断这个区间内有多少个素数,并逐一输出. def prime(m,n): list1=[] list2=[] for i in range(m,n+1): list1.append(i) for j in range(2,m/2): if i%j==0: list2.append(i) break #print

LeetCode -- Path Sum III分析及实现方法

LeetCode -- Path Sum III分析及实现方法 题目描述: You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must go downwards

163 AJAX Tab

复制代码 代码如下: // 163 AJAX Tab   // update 2006.10.18  // 增加鼠标延迟感应特性.  // update 2006.10.8  // A 标签 href 属性将保持原有HTML功能.增加urn属性为AJAX Load 路径.  // update 2006.10.11  // 修正IE5.0 undefined 未定义错误,增加脚本错误屏蔽  var Browser = new Object();  Browser.isMozilla = (typ

awk区间取值的例子

有如下的文本内容: ********* 123123 123123 ********* 321321 321321 ********* 456456 456456 ********* 123123 123123 ******** 匹配123,取出******区间的内容: awk -vRS="*+" '/123/{print RT$0}' file 说明: RS The input record separator, by default a newline(\n). RT The re

bat批处理一键登录网易163和126邮箱

相信很多朋友都有网易163或者是126的邮箱,如果你平时不怎么使用Foxmail这类邮件客户端工具来收发邮件,而是喜欢通过Web浏览器进行收发邮件,那么每次都要打开邮箱登录地址,然后输入用户名和密码,实在是麻烦.下面就介绍一种一键登录网易163和126邮箱的方法,打开并登录到163和126邮箱就像在桌面上用快捷方式打开应用程序那样简单. 一.一键登录网易163邮箱方法 将如下代码复制到记事本中,然后保存为.bat批处理文件,这里取名为:htmer_163mail.bat,保存后双击运行即可. 复

java中double转化为BigDecimal精度缺失的实例

java中double转化为BigDecimal精度缺失实例 @SuppressWarnings("static-access") public static void main(String[] args) { System.out.println(3215.10/2); BigDecimal bd = new BigDecimal(3215.10/2); System.out.println(bd); System.out.println(bd.setScale(2, bd.ROU

提示Outlook/Foxmail收取163邮件失败:ERR 您没有权限使用pop3功能

用outlook和foxmail总无法接收163邮件,汉,原来是他们不给我使用pop3功能了新申请的163,126邮箱用Jmail发送邮件老是不成功,用Outlook/Foxmail收取邮件也是失败,提示:ERR 您没有权限使用pop3功能.最终在http://help.163.com/找了原因: 复制代码 代码如下: "06年11月份后"新"申请的163免费邮箱暂时无法使用POP和SMTP功能,需要开通邮箱伴侣或参加一些不定期举办的活动后才可以使用客户端功能."

JAVA 时间区间的字符串合法性验证

复制代码 代码如下: String[] zone1="08:30-11:00".split("-");        String[] zone2="13:00-17:00".split("-");        String[] actzone="9:00-11:00".split("-"); DateFormat df = new SimpleDateFormat("hh:

Swift中switch语句区间和元组模式匹配

废话不多说了,下面一段代码给大家介绍了switch语句区间和元组模式匹配,具体内容如下所示: // switch 的广义匹配 let x = 1000 // 也就是说并没有像C语言那样 要求 switch 后面的是整数常量 switch x { // case后面可以跟区间啦 case 1...9: print("个位数") case 10...99: print("十位数") case 100...999: print("百位数") case

javascript仿163网盘无刷新文件上传系统

本来觉得这个系统会很复杂,但把每个部分都分析清楚后,其实需要的技术并不高.不过当我把各个功能函数都整理好准备进行封装时,却发现要把程序封装不是那么容易,因为程序跟html的耦合度太高.然后我逐步把程序中操作html相关的部分分离出来,首先把简单的分离,接着是文件列表,然后是file控件,最后是一些提示性程序.经过几次尝试才把整个结构封装好. 仿163网盘无刷新文件上传系统 .fu_list { width:600px; background:#ebebeb; font-size:12px; }