3 lines, 2ms Java solution


  • 7
    I
    public class Solution {
        public int titleToNumber(String s) {
            int answer = 0;
            for(char c: s.toCharArray()) answer = answer * 26 + (c - 'A' + 1);
            return answer;
        }
    }

  • 1

    Same as mine :)

    public class Solution {
        public int titleToNumber(String s) {
            int res = 0;
            for(char c : s.toCharArray()) {
                res = res * 26 + c - 'A' + 1;
            }
            return res;
        }
    }

  • 0
    D

    to my suprised my c code unexpectedly run 4ms exceed java code

    int titleToNumber(char* s) {
        int n ;
        for(n = 0;*s != '\0'; s++)
        {
           n =  26 * n + (*s - 'A' + 1);
       }
       return n;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.