Java 是否回文数字算法
lexus
阅读:93
2022-07-29 11:52:06
评论:0
题目:
判断一个数字是否是否回文数字示例 :输入: 121 返回值: true思路:字符串反转
代码:
public static void main(String[] args) { // 回文数字 int num = 1233212; } // 我的(这是可以写成一句的) public static boolean isPalindrome (int x) { // write code here String text = x + ""; StringBuilder sb = new StringBuilder(text); return text.equals(sb.reverse().toString()); } // 别人的 public static boolean isPalindrome (int x) { // write code here return String.valueOf(x).equals(new StringBuilder(String.valueOf(x)).reverse().toString()); } // 效率高的 public static boolean isPalindrome (int x) { // write code here //利用指针进行查找 String str= String.valueOf(x); int begin=0; int end=str.length()-1; while(begin<=end){ //如果相等,遍历下一个 if(str.charAt(begin)==str.charAt(end)){ begin++; end--; }else{ return false; } } return true; }
本文参考链接:https://www.cnblogs.com/mmdz/p/15608274.html
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。