Java Clean and Simple Code


  • 1
    F

    Do input validation first. Then traverse through the string to get result. '@' is before 'A', so we may just use "- '@'" to get the int value of current letter.

    public int titleToNumber(String s) {
        if (s == null || s.length() == 0) return 0;
        int res = 0;
        for (int i = 0; i < s.length(); i++) res = res * 26 + (s.charAt(i) - '@');
        return res;
    }

Log in to reply
 

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