前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#版[击败99.39%的提交] - Leetcode 171. Excel表列序号 - 题解

C#版[击败99.39%的提交] - Leetcode 171. Excel表列序号 - 题解

作者头像
Enjoy233
发布2019-03-05 15:46:39
4370
发布2019-03-05 15:46:39
举报

Leetcode 171. Excel表列序号 - 题解

Leetcode 171. Excel Sheet Column Number

在线提交: https://leetcode.com/problems/excel-sheet-column-number/

Description


Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

代码语言:javascript
复制
    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28 
    ...

Example 1:

代码语言:javascript
复制
Input: "A"
Output: 1

Example 2:

代码语言:javascript
复制
Input: "AB"
Output: 28

Example 3:

代码语言:javascript
复制
Input: "ZY"
Output: 701


思路:

这个问题实际上是要求将26进制字符串转为10进制数字,可看成是多项式展开, 不同位的权weigh是26的不同的幂次方。

已AC代码:

代码语言:javascript
复制
public class Solution
{
    public int TitleToNumber(string s)
    {
        int len = s.Length;
        int sum = 0;
        int weigh = 1;        // 该位的权, 26^0 ~ 26^(n-1)
        for (int i = len; i >= 1; i--)
        {
            sum += (s[i - 1] - 'A' + 1) * weigh;  // 从低位开始累加
            weigh *= 26;
        }
        return sum;
    }
}

Rank: You are here! Your runtime beats 99.39% of csharp submissions.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年07月01日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Leetcode 171. Excel表列序号 - 题解
  • Description
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档