Find Palindrome With Fixed Length LeetCode solution in Java

1
class Solution {
2
public long[] kthPalindrome(int[] queries, int intLength) {
3
long[] res = new long[queries.length];
4
for (int i = 0; i < queries.length; i++) {
5
res[i] = nthPalindrome(queries[i], intLength);
6
}
7
return res;
8
}
9

10
public long nthPalindrome(int nth, int kdigit) {
11
long temp = (kdigit & 1) != 0 ? (kdigit / 2) : (kdigit / 2 - 1);
12
long palindrome = (long) Math.pow(10, temp);
13
palindrome += nth - 1;
14
long res1 = palindrome;
15
if ((kdigit & 1) > 0) palindrome /= 10;
16
while (palindrome > 0) {
17
res1 = res1 * 10 + (palindrome % 10);
18
palindrome /= 10;
19
}
20
String g = "";
21
g += res1;
22
if (g.length() != kdigit) return -1;
23
return res1;
24
}
25
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0