A question LeeCode daily --Excel table column names

  [Introduction] adhere day more LeeCode brush title series

    short step, a thousand miles; not small streams into a mighty torrent. We would like to encourage each other and gentlemen!


  [Title] 168.Excel Table Column name

    Title 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 
		  ...

    Example:

    	示例 1:
    		输入: 1
			输出: "A"
 				  			 		
		示例 2:
			输入: 28
			输出: "AB"
			
	    示例 3:
			输入: 701
			输出: "ZY"。

    思路一:what? Is not that a binary conversion problem? What to see, jump jump. But when we look carefully you will find that although this is a binary conversion problem, but it's such a transformation and we usually transformed slightly different, for example:
         in decimal, the number 10, '1' represents the the actual number is 10, 10, when we use the expression '10'
         but in this case we are using the expression 27 when the 'AA', as expressed in the symbol 0 is not the present case, so the first 'a' the value of the 'Z' equal
         so in this case, how do we handle it? After reading code written you understand. Specific code as follows:

class Solution(object):
    def convertToTitle(self, n):
        """
        :type n: int
        :rtype: str
        """
        result_list = []
        while n:
            result_list.append(chr((n-1)%26+ord('A'))) 
          '''进制转化,注意此时先对n减1取余,而不是我们平常进制转化中的先取余再减一'''
            n = (n-1)//26
        return "".join(result_list[::-1])  

    operation result:
    Here Insert Picture Description

    说明:If you do not have to figure out just binary conversion, it may take a sample to try, contrasting with the usual practice in our decimal conversion with the current practice.


    Share on here, welcome to discuss the exchange.


    注明

    Topic Source: stay button (LeetCode)
    link: https: //leetcode-cn.com/problems/excel-sheet-column-title

Published 32 original articles · won praise 62 · views 1292

Guess you like

Origin blog.csdn.net/Mingw_/article/details/104803650