avatar
寻找下一个回文数# JobHunting - 待字闺中
d*w
1
Given a number n you have to find the nearest palindrome of n just greater
than n.
avatar
b*e
2
Split the string s into halves: s1 and s2.
If s1.concat(s1.reverse()) is bigger than s, then done.
Otherwise, let s1 be parseInt(s1) + 1, return s1.concat(s1.reverse()).
Two edge cases:
1. Case study the length of s: odd vs. even.
2. s1 is all '9'.
avatar
d*u
3
int find_next_palindrome(int a){
String b = Integer.toString(a);
int l = b.length()/2;
if (b.length()%2 == 1){
l++;
}
int c = (int)Math.pow(10, l);
int d = a/c;
String e=Integer.toString(d+1);
String re=reverse_string(e);//helper function
if (b.length()%2 == 1){
e+=b.charAt(b.length()/2);
}
e+=re;
return Integer.parseInt(e);
}
String reverse_string(String in){
char[] a=in.toCharArray();
for(int i=0,j=a.length-1;ichar c = a[i]; a[i]=a[j]; a[j]=c;
}
return String.valueOf(a);
}
avatar
b*e
4
In other words, s1 includes the middle char, as one of the edge case I
identified.

【在 d******u 的大作中提到】
: int find_next_palindrome(int a){
: String b = Integer.toString(a);
: int l = b.length()/2;
: if (b.length()%2 == 1){
: l++;
: }
: int c = (int)Math.pow(10, l);
: int d = a/c;
: String e=Integer.toString(d+1);
: String re=reverse_string(e);//helper function

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。