pull down to refresh
As a dev who rejects modernity and embrace tradition, this is my string based approach
public boolean isPalindrome(int x) {
String s = "" + x;
int l = s.length();
for(int i = 0;i < l;i++){
int j = l-1-i;
if (s.charAt(i)!=s.charAt(j)) return false;
}
return true;
}
and integer based approach
private int reverse(int n){
int r = 0;
while(n != 0) {
int d = n % 10;
r = r * 10 + d;
n = n / 10;
}
return r;
}
public boolean isPalindrome(int x) {
if(x<0)return false;
return reverse(x)==x;
}
reply
deleted by author
reply
reply
reply
reply
never lol
(defun print-palindrome-function ()
(println "public boolean isPalindrome(int x) {")
(println " String s = \"\" + x;")
(println " int l = s.length();")
(println " for (int i = 0; i < l; i++) {")
(println " int j = l - 1 - i;")
(println " if (s.charAt(i) != s.charAt(j)) return false;")
(println " }")
(println " return true;")
(println "}"))
(print-palindrome-function)
i32
->as_str()
->bytes()
(this is an iterator of bytes) ->rev()
(this makes the iterator iterate in reverse) ->collect()
(turns an iterator into a collection of the underlying type (bytes in this case).