c++ solution share


  • 0
    int largestPalindrome(int n) 
    {
        if (n == 1)
            return 9;
        
        int high = pow(10, n) - 1;
        int low = pow(10, n - 1);
        
        for (int i = high; i >= low; i--)
        {
            long long int pal = createPalindrome(i);
            for (int j = high; j >= low; j--)
            {
                if (pal / j > high)
                    break;
                if (pal % j == 0)
                    return pal % 1337;
            }
        }
    }
    
    long long int createPalindrome(int i)
    {
        long long int result;
        string str = to_string(i);
        string rev_str = str;
        reverse(rev_str.begin(), rev_str.end());
        str += rev_str;
       
        istringstream ist(str); 
        ist >> result;
        
        return result;
    }

Log in to reply
 

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