9.回文数
个人思考
首先从示例2可以看出符号也算在整数这个整体内,可以先判断整数若为负数则返回false 其次很容易就会想到遍历两次,从头以及从尾,遍历得到的结果相比较,相同则为回文数
public class Alee9 { public static void main ( String [ ] args) { int x = 121 ; Alee9 alee9 = new Alee9 ( ) ; System . out. println ( alee9. isPalindrome ( x) ) ; } public boolean isPalindrome ( int x) { if ( x< 0 ) { return false ; } String number = String . valueOf ( x) ; int [ ] array = new int [ number. length ( ) ] ; for ( int i = 0 ; i < number. length ( ) ; i++ ) { array[ i] = Character . getNumericValue ( number. charAt ( i) ) ; } StringBuilder sb1 = new StringBuilder ( ) ; StringBuilder sb2 = new StringBuilder ( ) ; for ( int i = 0 ; i < array. length; i++ ) { sb1. append ( array[ i] ) ; } for ( int j = array. length- 1 ; j >= 0 ; j-- ) { sb2. append ( array[ j] ) ; } String a = sb1. toString ( ) ; String b = sb2. toString ( ) ; if ( a. equals ( b) ) { return true ; } return false ; }
}