Small algorithm practicing --Excel table column names


title: Small algorithm training --Excel table column names
DATE: 2019-11-27 20:54:27
the Categories:

  • Algorithms
    tags:
  • easy

Excel table column names

description

Given a positive integer, it returns the name of the column corresponding to an Excel table.

E.g,

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

For example

Example 1:

Input: 1
Output: "A"

Example 2:

Input: 28
Output: "AB"

Example 3:

Input: 701
Output: "ZY"

Code

class Solution {
    public String convertToTitle(int n) {
        
		StringBuilder ans =new StringBuilder();
		while(n!=0) {
            n--;
            ans.append((char)('A'+n%26));
            n/=26;
		}
			return ans.reverse().toString();
    }
}

notes

This question is a base for the conversion problem, then we must first know what is the base for the conversion
of our common hexadecimal 10, binary
decimal is over 10 into a
binary into a full 2 1
At first glance, thought This question is filled into 26 1, written after the results found wrong.
In fact, this is not the usual binary conversion
you know, unusual binary conversion is from 0, 1 and here from the beginning.
L-> 26
AZ
'A' + (0-> 25)
so that the band 26 should be 0-25, input should be a 0, input 26 to give 25 fishes,
so Save a first n

Published 98 original articles · won praise 105 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_34691097/article/details/103283610