LeetCode 564. Find the Closest Palindrome

原题链接在这里:https://leetcode.com/problems/find-the-closest-palindrome/

题目:

Given an integer n, find the closest integer (not including itself), which is a palindrome.

The ‘closest‘ is defined as absolute difference minimized between two integers.

Example 1:

Input: "123"
Output: "121"

Note:

  1. The input n is a positive integer represented by string, whose length will not exceed 18.
  2. If there is a tie, return the smaller one as answer.

题解:

First find all the candidates, add "9999..999", "10000..0001".

Add first half, + reverse of (first half).

e.g. 12567, its first half is 125, the candiates couldbe 12521, 12421, 12621.

Then check all the candidates and find the closest one.

Time Complexity: O(mn). m = is candidates list size. n is candiate length.

Space: O(mn).

AC Java:

 1 class Solution {
 2     public String nearestPalindromic(String n) {
 3         if(n == null || n.length() == 0){
 4             return n;
 5         }
 6
 7         int len = n.length();
 8         if(len == 1){
 9             int val = Integer.valueOf(n);
10             return val > 0 ? "" + (val - 1) : "" + (val + 1);
11         }
12
13         List<String> cans = new ArrayList<>();
14
15         cans.add(allNine(len - 1));
16         cans.add(oneZero(len + 1));
17
18         int halfLen = (len + 1) / 2;
19         String sub = n.substring(0, halfLen);
20         if(len % 2 == 1){
21             cans.add(sub + new StringBuilder(sub).deleteCharAt(halfLen - 1).reverse().toString());
22             long halfVal = Long.valueOf(sub);
23             long plusOne = halfVal + 1;
24             cans.add("" + plusOne + new StringBuilder("" + plusOne / 10).reverse().toString());
25             long minusOne = halfVal - 1;
26             if(minusOne > 0){
27                 cans.add( "" + minusOne + new StringBuilder("" + minusOne / 10).reverse().toString());
28             }
29         }else{
30             cans.add(sub + new StringBuilder(sub).reverse().toString());
31             long halfVal = Long.valueOf(sub);
32             long plusOne = halfVal + 1;
33             cans.add("" + plusOne + new StringBuilder("" + plusOne).reverse().toString());
34             long minusOne = halfVal - 1;
35             if(minusOne > 0){
36                 cans.add("" + minusOne + new StringBuilder("" + minusOne).reverse().toString());
37             }
38         }
39
40         long diff = Long.MAX_VALUE;
41         String res = "";
42         long nValue = Long.valueOf(n);
43         for(String can : cans){
44             if(can.equals(n)){
45                 continue;
46             }
47
48             long canValue = Long.valueOf(can);
49             if(Math.abs(canValue - nValue) < diff){
50                 diff = Math.abs(canValue - nValue);
51                 res = can;
52             }else if(Math.abs(canValue - nValue) == diff && (res.length() == 0 || canValue < Long.valueOf(res))){
53                 res = can;
54             }
55         }
56
57         return res;
58     }
59
60     private String allNine(int len){
61         char [] charArr = new char[len];
62         Arrays.fill(charArr, ‘9‘);
63         return new String(charArr);
64     }
65
66     private String oneZero(int len){
67         char [] charArr = new char[len];
68         Arrays.fill(charArr, ‘0‘);
69         charArr[0] = ‘1‘;
70         charArr[len - 1] = ‘1‘;
71         return new String(charArr);
72     }
73 }

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12151375.html

时间: 2024-10-13 13:16:47

LeetCode 564. Find the Closest Palindrome的相关文章

LeetCode 564. Find the Closest Palindrome (构造)

题意: 给一个数字n 求离n最近(且不等)的回文串 存在多个答案返回最小的 首先很容易想到 将数字分为两段,如 12345 -> 123/45,然后将后半段根据前面的进行镜像重置 123/45 -> 12321 那,如果数字刚好是回文串,就把前半段-1就好了 但是存在以下例外,就是当前半段 +1 或 -1 就会造成进位 10999 10901-10999 = -98 11011 - 10999 = -12 可以发现是因为9存在的进位,同样0也可能,所以对前半段进行 +1 和 -1 两种处理,然

leetcode 564,546

564 Find the Closest Palindrome:给出一个长度不超过18的非负整数,求出与其最近接的回文整数(不包括它自己). 思路:假设其长度为偶数.将其分为两半,前一半设为$x$,那么最后的答案的前一部分一定是$x-1,x,x+1$三个中的一个.在这里,$x-1$有可能变成少一位的数字,比如100到99: $x+1$可能多一位,比如99到100.长度为奇数时类似的思路. long long strToInt(string s) { long long t=0; for(int

【leetcode刷题笔记】Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [ ["aa","b"], ["a","a","

[LeetCode] Find the Closest Palindrome 寻找最近的回文串

Given an integer n, find the closest integer (not including itself), which is a palindrome. The 'closest' is defined as absolute difference minimized between two integers. Example 1: Input: "123" Output: "121" Note: The input n is a po

【LeetCode】16. 3Sum Closest

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

[LeetCode][Python]16: 3Sum Closest

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 16: 3Sum Closesthttps://oj.leetcode.com/problems/3sum-closest/ Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target.Return the sum of

LeetCode解题报告--3Sum Closest

题目:与3数和最接近的和 Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given arr

Leetcode Array 16 3Sum Closest

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

【LeetCode】016 3Sum Closest

题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-