Skip to content

Commit 0108a40

Browse files
committed
fd
1 parent 600d7aa commit 0108a40

File tree

3 files changed

+48
-18
lines changed

3 files changed

+48
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@
377377
|547|[Friend Circles](https://leetcode.com/problems/friend-circles/)|[Java](leetcode/solution/src/FriendCircles.java)|70||
378378
|557|[Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/)| [Java](leetcode/solution/src/ReverseWordsInAStringIII.java)|100||
379379
|560|[Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/)| [Java](leetcode/solution/src/SubarraySumEqualsK.java)|70||
380+
|564|[Find the Closest Palindrome](https://leetcode.com/problems/find-the-closest-palindrome/)|[Java](leetcode/solution/src/FindTheClosestPalindrome.java)|80|此题暴力法即可|
380381
|606|[Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree/)|[Java](leetcode/solution/src/ConstructStringFromBinaryTree.java)|90||
381382
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)|[Java](leetcode/solution/src/MergeTwoBinaryTrees.java)|100|很简单|
382383
|621|[Task Scheduler](https://leetcode.com/problems/task-scheduler/)|[Java](leetcode/solution/src/TaskScheduler.java)|70||
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class FindTheClosestPalindrome {
2+
3+
/**
4+
* 此题只给出暴力法
5+
* 此种题目太math了,一般不会问
6+
*/
7+
public String nearestPalindromic(String n) {
8+
long val = Long.parseLong(n);
9+
for (int i = 0; ; i++) {
10+
long k1 = val - i, k2 = val + i;
11+
if (isPalindrome(k1)) {
12+
return String.valueOf(k1);
13+
}
14+
if (isPalindrome(k2)) {
15+
return String.valueOf(k2);
16+
}
17+
}
18+
}
19+
20+
private boolean isPalindrome(long k) {
21+
long x = k, rev = 0;
22+
for ( ; k > 0; ) {
23+
rev = rev * 10 + k % 10;
24+
k /= 10;
25+
}
26+
return rev == x;
27+
}
28+
}

leetcode/src/Main.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,33 @@ public class Main {
44

55
public static class Solution {
66

7-
8-
9-
}
10-
11-
class KthLargest {
12-
13-
Queue<Integer> queue = new PriorityQueue<>();
14-
int capacity;
15-
16-
public KthLargest(int k, int[] nums) {
17-
capacity = k;
18-
for (int n : nums) {
19-
queue.offer(n);
7+
public String nearestPalindromic(String n) {
8+
long val = Long.parseLong(n);
9+
for (int i = 0; ; i++) {
10+
long k1 = val - i, k2 = val + i;
11+
if (isPalindrome(k1)) {
12+
return String.valueOf(k1);
13+
}
14+
if (isPalindrome(k2)) {
15+
return String.valueOf(k2);
16+
}
2017
}
2118
}
2219

23-
public int add(int val) {
24-
queue.offer(val);
25-
while (queue.size() > capacity) {
26-
queue.poll();
20+
private boolean isPalindrome(long k) {
21+
long x = k, rev = 0;
22+
for ( ; k > 0; ) {
23+
rev = rev * 10 + k % 10;
24+
k /= 10;
2725
}
28-
return queue.peek();
26+
return rev == x;
2927
}
28+
3029
}
3130

3231
public static void main(String[] args) {
3332
Solution solution = new Solution();
33+
String s = solution.nearestPalindromic("807045053224792883");
34+
System.out.println(s);
3435
}
3536
}

0 commit comments

Comments
 (0)