One line by Java 8 Stream API:


  • 0
    X
    public int titleToNumber(String s) {
        // int sum = 0;
        // for (int i = 0, len = s.length(); i < len; i++) {
        //     sum = sum * 26 + (s.charAt(i) - 'A' + 1);
        // }
        // return sum;
        
        return s.chars().reduce(0, (r, ch) -> r * 26 + (ch - 'A' + 1));
    }
    

Log in to reply
 

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