LeetCode—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进制

#include <string>
using namespace std;

	string convertToTitle(int n) {
		int i,j,mod;
		string s;
		char num;
		while(n>0)
		{
			n--;
			mod=n%26;
			num='A'+mod;
			s=num+s;
			n=n/26;
		}
		return s;
	}

void main()
{
	string x = convertToTitle(28);
}

反过来还有个相关的问题:

For example:

    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28 

这里主要的问题是在将string当中的元素一个个取出来,可以利用像数组一样的方法

#include <string>
using namespace std;

int titleToNumber(string s) {
	int length = s.size();
	int retVal = 0;
	for(int i = 0; i < length; i++)
	{
		int x = (s[i]-'A')+1;
		retVal =retVal*26+x;
	}
	return retVal;
}

void main()
{
	int x = titleToNumber("AB");
}
时间: 2024-10-11 03:25:18

LeetCode—Excel Sheet Column Title的相关文章

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

[LeetCode] Excel Sheet Column Title 求Excel表列名称

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 adding this problem and creating all test

LeetCode Excel Sheet Column Title (输出excel表的列名称)

题意:给一个数字n,输出excel表的列名称. 思路:其实观察可知道,是个26进制的标记而已.那就模拟一下,每次计算一位时就先左移1位,再进行计算. 1 class Solution { 2 public: 3 string convertToTitle(int n) { 4 string ans=""; 5 while(n) //26进制,但是输出时不同而已,是从1开始,而不是0.每次计算将其先左移1位即可. 6 { 7 ans+=(n-1)%26+'A'; 8 n=(n-1-(n-

LeetCode - Excel Sheet Column Title

时隔几分钟又来写一个题,这个应该算个水题. public class Solution { public String convertToTitle(int n) { StringBuilder ans = new StringBuilder(); if(n < 1) ans.append(""); else { while(n-- > 0) { char ch = (char) (n % 26 + 'A'); ans.append(ch); n /= 26; } } re

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

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

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

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