My 8 line java solution 168ms


  • 0
    S
    public class Solution {
    	public String convertToTitle(int n) {
    		String s = "";
    		String map = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    	        n--;
    	        while (!(n<0)) {
    	    	      s = map.charAt(n%26) + s;
    	    	      n = n/26-1;
    	         }
    	    
    	        return s;
    	}
    }

  • 0
    H

    that is quite good idea.

    Here is mine:

    public class Solution {
    public String convertToTitle(int n) {
    String s="";

        while(n>0){
            s=(char)((n-1)%26+65)+s;
            n=(n-1)/26;
        }
        return s;
    }
    

    }


Log in to reply
 

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