package com.interview.misc;

/**
 * http://www.careercup.com/question?id=6139456847347712
 * Test case : 0 or negative number
 */
public class ConvertNumberIntoBase26 {

    /**
     * Here 1..26 is A...Z
     * 27...52 is AA...AZ. So we first try to divide num by 26 then find ceiling...so 27...52 returns 2. Then we subtract 1 from the number
     * @param num
     * @return
     */
    public String numberToBase26(int num){
        StringBuffer buff = new StringBuffer();
        while(num > 0){
            char r  = convert(num % 26);
            buff.append(r);
            num = (int)(Math.ceil(num*1.0/26));
            num--;
        }
        return buff.reverse().toString();
    }
    
    public int base26ToNumber(String str){
        int result = 0;
        int pow = 1;
        for(int i = str.length() -1; i>=0; i--){
            char s = str.charAt(i);
            s = (char)(s - 'A'+1);
            result += s*pow;
            pow *= 26;
        }
        return result;
    }
    
    private char convert(int r){
        if(r == 0){
            return 'Z';
        }else{
            return (char)(r + 'A'-1);
        }
    }
    
    public static void main(String args[]){
        ConvertNumberIntoBase26 cni = new ConvertNumberIntoBase26();
        System.out.println(cni.numberToBase26(704));
        System.out.println(cni.base26ToNumber("AAB"));

        System.out.println(cni.numberToBase26(52));
        System.out.println(cni.base26ToNumber("AZ"));

        System.out.println(cni.numberToBase26(13));
        System.out.println(cni.base26ToNumber("M"));

        System.out.println(cni.numberToBase26(126));
        System.out.println(cni.base26ToNumber("DV"));
    }
}

@@ -33,357 +33,203 @@
 %0A * 
-http://www.careercup.com/question?id=6139456847347712%0A * Test case : 0 or negative number%0A */%0Apublic class ConvertNumberIntoBase26 %7B%0A%0A    /**%0A     * Here 1..26 is A...Z%0A     * 27...52 is AA...AZ. So we first try to divide num by 26 then find ceiling...so 27...52 returns 2. Then we subtract 1 from the number%0A     * @param num%0A     * @return%0A     */
+Given a positive integer, return its corresponding column title as appear in an Excel sheet.%0A * https://leetcode.com/problems/excel-sheet-column-title/%0A */%0Apublic class ConvertNumberIntoBase26 %7B%0A
 %0A   
@@ -267,11 +267,10 @@
 nt n
-um
 )
+ 
 %7B%0A  
@@ -332,17 +332,17 @@
 hile
+ 
 (n
-um
  %3E 0)
+ 
 %7B%0A  
@@ -355,35 +355,11 @@
     
-char r  = convert(num %25 26)
+n--
 ;%0A  
@@ -380,17 +380,36 @@
 .append(
-r
+(char)(n %25 26 + 'A')
 );%0A     
@@ -420,60 +420,17 @@
    n
-um
  = 
-(int)(Math.ceil(num*1.0/26));%0A            num--
+n / 26
 ;%0A  
@@ -531,16 +531,17 @@
 ing str)
+ 
 %7B%0A      
@@ -613,16 +613,17 @@
 ngth() -
+ 
 1; i%3E=0;
@@ -627,16 +627,17 @@
 =0; i--)
+ 
 %7B%0A      
@@ -796,163 +796,8 @@
   %7D%0A
-    %0A    private char convert(int r)%7B%0A        if(r == 0)%7B%0A            return 'Z';%0A        %7Delse%7B%0A            return (char)(r + 'A'-1);%0A        %7D%0A    %7D%0A    
 %0A   
@@ -835,16 +835,17 @@
  args%5B%5D)
+ 
 %7B%0A      

update base 26 qs