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;
}

时间: 2024-10-09 22:32:06

leetcode第171题-Excel Sheet Column Number的相关文章

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

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,

leetcode:171 Excel Sheet Column Number-每日编程第十题

Excel Sheet Column Number Total Accepted: 54683 Total Submissions: 140674 Difficulty: Easy 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 -

LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number

100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} p * @param {TreeNode} q * @return {boolean} */ var isSameTree = function(p, q) { if(p

leetcode——171 Excel Sheet Column Number (表格字母与整数的映射)

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 Hide Tags: M

171. Excel Sheet Column Number(C++)

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 题目大意: 相

171. Excel Sheet Column Number

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 static publi

[LeetCode] Excel Sheet Column Number

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 解题思路: 看成是进制转

LeetCode Excel Sheet Column Number 表列数

题意:天啊!我竟然看不懂题意,还去翻别人的代码才懂!给定一个字符串,求该字符串二十六进制的总值. 思路:'A'~'Z'就是1到26,"AA"=26+1=27,"BA"=26*2+1=53,"CBA"=26*26*3+26*2+1.相当于321(十进制),那么就是10*10*3+10*2+1,说第3条式子,C在第3位,在第1位时就是1~26的值了,也就是3,在第2位时就是将3*26了,再第3位时就是将3*26*26了:同理B就是2*26啦,A就是1