Answer mismatch for Problems Excel Sheet Column Number and Excel Sheet Column Title


  • 0
    K

    Ran the following code which are both accepted (Problem_Names: Excel Sheet Column Number and Excel Sheet Column Title) and yet there is an answer mismatch which should not be.
    I also added the solution which I think was correct.
    I might be wrong and missing something so please help.

    string convertToTitle(int n)
    {
        string ans="";
        int d = n;
        while(d>0)
        {
            int int_ans=0;
            char ch;
            int_ans = d%26;
            d = d/26;
            if(int_ans==0)
            {
                d=d-1;
                int_ans=26;
            }
            ch = char(int_ans+64);
            ans = ch + ans;
        }
        return ans;
    }   
     int titleToNumber(string s) {
        int l= s.size(), n=0;
        for(int i=0; i<l; i++)
            n=n+(int(s[i])-64)*(pow(26, (s.size()-i-1)));
        return n;
        }
    int main(){
    for (int i=0; i<100; i++)
        {
            cout<<i<<":"<<convertToTitle(i)<<":"<<titleToNumber(convertToTitle(i))<<endl;
        }
    return 0;
    }
    

    Correct solution according to me:

    int titleToNumber(string s){
        int l= s.size(), n=0;
        for(int i=0; i<l; i++)
            n=n+(int(s[i])-64)*(pow(26,(s.size()-i-1)))+i;
    
        return n;
        }

Log in to reply
 

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