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

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

Hide: Tags Math

解题思路:

(1)找到对应关系,26进制,1对应A(对应的ASCII码为65)

(2)不难看出规律为: Char temp=(n-1)%26+‘A‘ ,或者 Char temp=(n-1)%26+65

(3)使用while循环进行对应位的分离与处理

代码如下:

	public static String convertToTile(int n)
	{
		String res="";
		while (n!=0)
		{
			char temp=(char)((n-1)%26+'A');
			res=res+temp;
			n=(n-1)/26;
		}
		return res;
	}
时间: 2024-11-06 08:14:16

leetcode——168 Excel Sheet Column(整数与表格字母的映射)的相关文章

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

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

Java for LeetCode 168 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 解题思路: JAVA实现如下: static public String convertToTitle(int n) { S

Leetcode 168 Excel Sheet Column Title 进制数转化

题意:将数字转化成excel表中的行中的项目 本质是10进制转化为26进制,但是在中间加入了一个不一样的操作,在每次操作前都需要n-- 1 class Solution { 2 public: 3 string convertToTitle(int n) { 4 string s(""); 5 for(; n--; n/=26){ 6 s += string("A"); 7 s[s.size() - 1] += n% 26; 8 } 9 reverse(s.begi

LeetCode OJ: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 Solution {

[C++]LeetCode: 116 Excel Sheet Column Number (excel列坐标转数字)

题目: 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 思路:这道题和Excel Sheet Column Title和是对

【LeetCode】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 思路: 其实就是个进制转换.水水就过.倒是 Python 的代码让我

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[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 class Solution { public: int titleToNu