public class Solution {
static int v;
public static boolean isPalindrome(int x) {
//optimizations
if(x<0) return false;
if(x<10) return true;
if(x%10==0) return false;
if(x<100&&x%11==0) return true;
if(x<1000&&((x/100)*10+x%10)%11==0) return true;
//actual logic
v=x%10;
x=x/10;
while(xv>0)
{
v=v*10+x%10;
x/=10;
}
if(v>x){v/=10;}
return v==x?true:false;
}
}
[9 ms JAVA] Beats 99.5 java solutions, easy to understand




@yuwang3 It would be caused by the updates of test cases?... I had one solution for another problem which was [3 ms] 6 months ago but [35 ms] for now, even though I just copy and paste my previous code.


@JunchenSu You can submit the code more than once. Sometimes the performance of server is very slow and then you can get different result.

@zhugejunwei And as Leetcode officially pronounced, we cannot use static for Leetcode questions.

"Extra Space": To understand this you need to understand what constant space means in terms of an algorithm.
Constant space means that the amount of space that your algorithm uses is independent of the input parameters. Say you are given an array of size n. If the amount of space your algorithm uses scales with n, then it's not constant. If your algorithm always uses a fixed amount of space (5 variables, an array of fixed size: 100, 300, 5000, etc..), you are golden. [ ref: https://discuss.leetcode.com/topic/429/meaningofconstantspacecomplexity/2 ]
In this algorithm, the complexity is
O(d)
whered
is the number of digits in integerx
. Integerv
is not related tod
in any way, thus this is a constant space solution.Static functions/variable:
https://leetcode.com/faq/ warns against using them and they should be avoided in any production ready(real life) code, but there is no guideline for not using them. As long as the code passes the tests it's okay. It's the algorithm that matters.
