Share my 11-line c++ solution


  • 0
    Y
    class Solution {
        long create(int n){
            string s = to_string(n), ss = s;
            reverse(s.begin(),s.end());
            return stol(ss+s);
        }
    public:
        int largestPalindrome(int n) {
            if (n==1) return 9;
            long lo = pow(10,n-1), hi = pow(10,n)-1, num = hi*hi/pow(10,n), val;
            while (1){
                val = create(num--);
                for (int j=hi; j>=lo; --j){
                    if (val%j==0) return val%1337;
                    if (j*hi < val) break;
                }
            }
            return val%1337;
        }
    };
    

Log in to reply
 

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