Straightforward java solution


  • 0
    L
    public String convertToTitle(int n) {
        StringBuilder res = new StringBuilder();
        while(n != 0){
            int tail = n % 26;
            if(tail == 0){
                res.insert(0,'Z');
                n = n/26 - 1;
            }
            else{
                res.insert(0,(char)('A' + tail - 1));
                n = n/26;
            }
        }
        return res.toString();
    }

  • 0

    Very Simple Java Solution

    public static String convertToTitle(int n) {
    		StringBuffer sb = new StringBuffer();
    		int c;
    		while (n > 0) {
    			c = n % 26;
    			if (c == 0) {
    				sb.append('Z');
    				n = 0;
    			} else {
    				sb.append((char) ('A' + c - 1));
    				n = n / 26;
    			}
    		}
    		return sb.reverse().toString();
    	}

  • 0
    L

    input n = 52 your algorithom is wrong.


Log in to reply
 

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