LeetCode //C - 171. Excel Sheet Column Number

168. Excel Sheet Column Title

Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number.

For example:

A -> 1

B -> 2

C -> 3

...

Z -> 26

AA -> 27

.AB -> 28

...

Example 1:

Input: columnTitle = "A"
Output: 1

Example 2:

Input: columnTitle = "AB"
Output: 28

Example 3:

Input: columnTitle = "ZY"
Output: 701

Constraints:
  • 1 <= columnTitle.length <= 7
  • columnTitle consists only of uppercase English letters.
  • columnTitle is in the range ["A", "FXSHRXW"].

From: LeetCode

Link: 171. Excel Sheet Column Number


Solution:

Ideas:
  1. The function titleToNumber takes a string columnTitle as input.
  2. It initializes an integer result to 0.
  3. It iterates over each character in the string. For each character, it updates the result by multiplying the current result by 26 (since there are 26 letters in the alphabet) and adding the position of the current character in the alphabet (calculated as *columnTitle - 'A' + 1).
  4. The function returns the final result, which is the corresponding column number.
Code:
c 复制代码
int titleToNumber(char* columnTitle) {
    int result = 0;
    while (*columnTitle) {
        result = result * 26 + (*columnTitle - 'A' + 1);
        columnTitle++;
    }
    return result;
}
相关推荐
筱姌21 分钟前
单调队列优化DP——AcWing 135. 最大子序和
算法
橙子1991101624 分钟前
算法题 — 接雨水
前端·javascript·算法
硕风和炜31 分钟前
【LeetCode:2742. 给墙壁刷油漆 + 递归 + 记忆化搜索 + dp】
java·算法·leetcode·缓存·dp·记忆化搜索·递归
每天努力进步!1 小时前
LeetCode热题100刷题1:1.两数之和、49. 字母异位词分组、128. 最长连续序列
c++·算法·leetcode·哈希表
幸运草时代狂想曲1 小时前
67.二进制求和
数据结构·python·算法·leetcode·力扣
大钻石选莱驰1 小时前
NEFU算法设计与分析课程设计
算法
人间花木2 小时前
快速排序(C/C++实现)—— 简单易懂系列
c语言·c++·算法·快速排序·入门教学
羚通科技2 小时前
视频共享融合赋能平台LntonCVS安防监控平台现场方案实现和应用场景
数据库·人工智能·算法·安全·音视频·智慧城市
GY35093 小时前
C#中实现按位域操作
开发语言·算法·c#
起名字真南3 小时前
【C语言】--操作符详解
c语言·开发语言·算法·visual studio