9. 回文数

it2025-10-17  8

9. 回文数

//代码一:直接用字符串来判断,不过时间和空间消耗很多。 class Solution { public boolean isPalindrome(int x) { String s = String.valueOf(x); StringBuffer sb =new StringBuffer(s); if(s.equals(sb.reverse().toString())){ return true; }else{ return false; } } } //代码二 class Solution { public boolean isPalindrome(int x) { int n = 0; int m =x; if(x<0){ return false; } while(x!=0){ n = n*10 + x%10; x = x/10; } return m == n; } }
最新回复(0)