我需要找到输入的下一个回文,使得数字不超过1000000 DIGITS。 为此,我正在使用BigInteger,并且正在获取“超出时间限制”。在java中使用reverse()与BigInteger
现在该做什么?
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
class Ideone
{
static boolean palindrome(BigInteger a)
{
String b=""+a;
StringBuffer s=new StringBuffer(b);
StringBuffer c=s.reverse();
String d=c.toString();
return d.equals(b);
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
BigInteger k=sc.nextBigInteger();
try{for(BigInteger i=k.add(BigInteger.ONE);;i=i.add(BigInteger.ONE)){
if(palindrome(i)){System.out.println(""+i); break;}
}//for
}catch(Exception e){}
}//wh
}
}
TLE?什么是? - –
@MarcoAcierno超出时间限制。 –
如果此代码有效,您可以将其发布到CodeReview。我会开始使用valueOf缓存至少一些值,为什么你将它们转换为字符串? –