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

前端 未结 28 1321
[愿得一人]
[愿得一人] 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:40

    Another Java:

    public static int convertNameToIndex(String columnName) {
        int index = 0;
        char[] name = columnName.toUpperCase().toCharArray();
    
        for(int i = 0; i < name.length; i++) {
            index *= 26;
            index += name[i] - 'A' + 1;
        }
    
        return index;
    }
    

提交回复
热议问题