LeetCode //C - 168. Excel Sheet Column Title

168. Excel Sheet Column Title

Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.

For example:

A -> 1
B -> 2
C -> 3

Z -> 26
AA -> 27
AB -> 28

 

Example 1:

Input: columnNumber = 1
Output: “A”

Example 2:

Input: columnNumber = 28
Output: “AB”

Example 3:

Input: columnNumber = 701
Output: “ZY”

Constraints:
  • 1 < = c o l u m n N u m b e r < = 2 31 − 1 1 <= columnNumber <= 2^{31} - 1 1<=columnNumber<=2311

From: LeetCode
Link: 168. Excel Sheet Column Title


Solution:

Ideas:

1. Memory Allocation: We allocate memory for the result string. Since column numbers are large, we assume a reasonable size (8 characters) to cover most cases.

2. Loop through the column number: While columnNumber is greater than 0:

  • Decrement columnNumber by 1 to shift it to a 0-based index.
  • Compute the remainder when columnNumber is divided by 26. This gives us the position in the alphabet.
  • Convert this remainder to the corresponding character by adding it to the ASCII value of ‘A’.
  • Store the character in the result array and increment the index.
  • Update columnNumber by integer division by 26.

3. String Reversal: After forming the result, the string is reversed because characters are added from the least significant digit to the most significant digit.

4. Return the Result: The result is returned after null-terminating it.

Code:
char* convertToTitle(int columnNumber) {
    char* result = (char*)malloc(8 * sizeof(char));  // Allocate memory for the result
    int index = 0;
    
    while (columnNumber > 0) {
        columnNumber--;  // Adjust to 0-indexed
        int remainder = columnNumber % 26;
        result[index++] = 'A' + remainder;
        columnNumber /= 26;
    }
    
    result[index] = '\0';  // Null-terminate the string
    
    // Reverse the string
    int len = strlen(result);
    for (int i = 0; i < len / 2; i++) {
        char temp = result[i];
        result[i] = result[len - 1 - i];
        result[len - 1 - i] = temp;
    }
    
    return result;
}

相关推荐

  1. 面试算法-168-LRU 缓存

    2024-06-07 14:28:05       31 阅读
  2. Ubuntu16-18网卡配置

    2024-06-07 14:28:05       30 阅读
  3. _198打家劫舍

    2024-06-07 14:28:05       60 阅读
  4. Leetcode--198

    2024-06-07 14:28:05       40 阅读
  5. Leetcode 169

    2024-06-07 14:28:05       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 14:28:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 14:28:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 14:28:05       82 阅读
  4. Python语言-面向对象

    2024-06-07 14:28:05       91 阅读

热门阅读

  1. 解决Vscode Copilot连不上网问题

    2024-06-07 14:28:05       28 阅读
  2. 微信小程序计算器

    2024-06-07 14:28:05       25 阅读
  3. 微信小程序上线后获取定位失效

    2024-06-07 14:28:05       31 阅读
  4. 用ffmpeg对视频添加语音、背景音乐和字幕的方法

    2024-06-07 14:28:05       35 阅读
  5. Unity3D DOTS JobSystem物理引擎的使用详解

    2024-06-07 14:28:05       30 阅读
  6. Linux systemctl:掌握软件启动和关闭的利器

    2024-06-07 14:28:05       30 阅读
  7. 探索Linux中的`aserver`命令(假设命令)

    2024-06-07 14:28:05       28 阅读
  8. 生活中优秀学习习惯

    2024-06-07 14:28:05       30 阅读
  9. rust的类型转换和一些智能指针用法(四)

    2024-06-07 14:28:05       27 阅读