Judege whether a number's binary expression is palidrom
2 posters
Page 1 of 1
Judege whether a number's binary expression is palidrom
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;
}
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;
}
Re: Judege whether a number's binary expression is palidrom
boolean isPalindrome(int n) {
for (int i=0; i<16; i++) {
if ((n>>i) != (n>>(31-i))) return false;
}
return true;
}
for (int i=0; i<16; i++) {
if ((n>>i) != (n>>(31-i))) return false;
}
return true;
}
Re: Judege whether a number's binary expression is palidrom
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
Similar topics
» you have an array of integers, find the longest subarray which consists of numbers that can be arranged in a sequence
» Jude whether a binary tree is well ordered
» To get mirror image of a binary tree
» Write a program to determine if a binary tree is well ordered
» Describe an algorithm to save a Binary Search Tree (BST) to a file
» Jude whether a binary tree is well ordered
» To get mirror image of a binary tree
» Write a program to determine if a binary tree is well ordered
» Describe an algorithm to save a Binary Search Tree (BST) to a file
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|