Programming Riddle: How might you translate an Excel column name to a number?

前端 未结 28 1325
[愿得一人]
[愿得一人] 2020-11-29 22:26

I was recently asked in a job interview to resolve a programming puzzle that I thought it would be interesting to share. It\'s about translating Excel column letters to actu

28条回答
  •  遥遥无期
    2020-11-29 22:56

    Easy Java solution -->

    public class ColumnName {
    
    public static int colIndex(String col)
    {   int index=0;
        int mul=0;
        for(int i=col.length()-1;i>=0;i--)
        {   
            index  += (col.charAt(i)-64) * Math.pow(26, mul);
            mul++;
        }
        return index;
    }
    
    public static void main(String[] args) {
    
        System.out.println(colIndex("AAA"));
    
    }
    

提交回复
热议问题