Find Palindrome With Fixed Length LeetCode solution in C++
1#define ll long long2class Solution {3public:4vector<long long> kthPalindrome(vector<int> &queries, int intLength) {5vector<ll> result;6ll start = intLength % 2 == 0 ? pow(10, intLength / 2 - 1) : pow(10, intLength / 2);7for (int q : queries) {8string s = to_string(start + q - 1);9string palindrome = s;10reverse(s.begin(), s.end());11if (intLength % 2 == 0) {12palindrome += s;13} else {14palindrome += s.substr(1, s.size() - 1);15}1617// len of palindrome should be intLength, otherwise -118if (palindrome.size() == intLength)19result.push_back(stoll(palindrome));20else21result.push_back(-1);22}2324return result;25}26};