Judege whether a number's binary expression is palidrom

View previous topic View next topic Go down

Judege whether a number's binary expression is palidrom

Post  Admin on Mon Aug 29, 2011 10:11 pm

public static boolean judgepalidrom(int num){
char[] ch=new char[Integer.size];
for(int i=0;i<ch.length();i++){
char[Integer.size-i-1]=(char)((num>>i)&1+'0');
}
String str=new String(ch);
for(int j=0;j<str.length();j++){
if(str[j]!=str[str.length()-j-1])
return false;
}
return true;
}

Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

Re: Judege whether a number's binary expression is palidrom

Post  Admin on Tue Aug 30, 2011 10:35 am

boolean isPalindrome(int n) {
for (int i=0; i<16; i++) {
if ((n>>i) != (n>>(31-i))) return false;
}
return true;
}

Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

Re: Judege whether a number's binary expression is palidrom

Post  viterbi on Thu Sep 15, 2011 11:32 pm

This is not correct. You probably misunderstood the meaning of >>

Admin wrote:boolean isPalindrome(int n) {
for (int i=0; i<16; i++) {
if ((n>>i) != (n>>(31-i))) return false;
}
return true;
}

viterbi

Posts : 32
Join date : 2011-09-03

View user profile

Back to top Go down

Re: Judege whether a number's binary expression is palidrom

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum