leetcode第168题-Excel Sheet Column Title

本题目与前面的把序列字母转换为数字的题目正好相反,是把数字转化为对应的序列字母。首先是步骤:根据逐步求商先确定字母有多少个,再动态申请空间避免内存浪费*(s+size-i)就对应于s[size-i],从后往前逐步的给每个字母赋值即可。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char *convertToTitle(int n) {
    char *s;
    int fac=26,size=0,num=n,i=1;
    while(num>0) {
        num=(num - 1) / 26;
        size++;
    }
    s=(char*)malloc(size*sizeof(char));
    while(n>0){
        n--;
        *(s+size-i)=(char)(n%fac+'A')+*s;
        n/=fac;
        i++;
    }
    return s;
}
int main()
{
	int x;
	while(scanf("%d",&x)!=EOF)
	{
		printf("%s\n",convertToTitle(x));
	}
	return 0;
}

时间: 2024-10-04 02:50:55

leetcode第168题-Excel Sheet Column Title的相关文章

leetcode_168题——Excel Sheet Column Title(数学问题)

Excel Sheet Column Title Total Accepted: 25652 Total Submissions: 142582My Submissions Question Solution 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

leetcode第171题-Excel Sheet Column Number

这道题是把字符串类型的类似于表格的字母对应到数字,题目意思很简单,我一开始还很把每个字母映射到数字,后来看到别人用这个方法计算每个字母的值s[i]-'A',这么简单的方法不用简直是可惜. int titleToNumber(char *s) { int sum=0,i; int len=strlen(s); for(i=0;i<len;i++) sum=sum*26+(s[i]-'A'+1); return sum; }

168.Excel Sheet Column Title

Excel Sheet Column Title 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 注意:由于下标从1开始而不是从0开始,因此要减一操作. static public string Convert

leetcode Excel Sheet Column Title python

Excel Sheet Column Title 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 python code class Solution: # @param {integer} n # @ret

[leetcode] Excel Sheet Column Title &amp; Excel Sheet Column Number

Excel Sheet Column Title 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进制转2进制一样. 题解: class Solution

2016.5.19——Excel Sheet Column Title

Excel Sheet Column Title 本题收获: 1.由int型转换为整型(string),如何转化, res = 'A'+(n-1)%26和之前由A-z转化为十进制相反,res = s[i]-'A'+1.(为什么有+1,-1还有点迷糊,貌似是十进制是0-9,26进制是) 2.十进制到26进制转化 题目: Given a positive integer, return its corresponding column title as appear in an Excel shee

Excel Sheet Column Number &amp; Excel Sheet Column Title

1. 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 1 class S

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 ... AAA -> 703 AAB -> 704 Cre

LeetCode168——Excel Sheet Column Title

LeetCode168--Excel Sheet Column Title 题目 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 Credits: Special thanks to @ifanchu for