• 每日一码——回文数判断


           今天的每日一码的题目讲的是判断一个数是不是回文数,所谓的回文数就是不论是从左往右读还是从右往左读都是一样的结果,比方说12321。方法有很多,这里和大家一起分享几个。

           主要可以从两个方面来解决吧:一个是把输入看成是一串字符串,然后利用字符串的一些函数来进行处理。个人觉得这类方法可以记一记,在面试的时候也经常会遇到字符串的处理编程。

          还有一种是对这个整数进行处理,将最高位与最低位进行比较,这样一步一步进行下去。


    第一种方法:


     

    public class palindromeNumber {
    public static void main(String[] args) {
    /*
    * 从控制台输入数字 调用isPalindrome()方法判断 输出判断结果
    */
    Scanner scan = new Scanner(System.in);
    System.out.println("请输入数字:");
    String strNum = scan.next();
    // 当作字符串处理
    System.out.println(isPalindrome(strNum));
    }

    public static boolean isPalindrome(String str) {
    boolean result = false;
    for(int i = 0; i < str.length() / 2; i++) {
    // 利用charAt()函数返回指定索引处的字符
    if (str.charAt(i) == str.charAt(str.length() - 1 - i)) {
    result = true;
    } else {
    result = false;
    }
    }
    return result;
    }
    }

          本方法最关键的点是利用charAt()方法对字符串每一对对应的字符位进行判断,如果有一对对应位上字符不相等,那么结果就为false。


    第二种方法:


     

    public class palindromeNumber {
    public static void main(String[] args) {
    /* 输入数字 字符串反转并判断 输出判断结果 */
    Scanner scan = new Scanner(System.in);
    System.out.println("请输入数字:");
    String strNum1 = scan.next();
    System.out.println(isPalindrome(strNum1));
    }

    public static boolean isPalindrome(String strNum) {
    boolean result = false;
    StringBuilder strNum2 = new StringBuilder(strNum);
    strNum2.reverse();//字符串反转并判断
    for (int i = 0; i < strNum.length() / 2; i++) {
    if (strNum.charAt(i) != strNum2.charAt(i)) {
    result = false;
    } else {
    result = true; } }
    return result;
    }}

    第二种方法与第一种基本上是一样的,只不过这里还利用了字符串的reverse()方法,对于字符串的知识在公众号的java基础中也有深入的剖析。这里提示一下,String类是不可变的,而StringBuilder是可变字符序列。更多的记得查询资料哦!


    第三种方法:


     

    public class palindromeNumber {
    public static void main(String[] args) {
    /*
    * 从控制台输入数字 调用isPalindrome()方法判断 输出判断结果
    */
    Scanner scan = new Scanner(System.in);
    System.out.println("请输入数字: ");
    int num = scan.nextInt();
    System.out.println(

    isPalindrome(num));
    }

    public static boolean isPalindrome(int num) {
    //将整数转换成字符串,并反转作比较
    return String.valueOf(num).equals(new StringBuffer(num + "").reverse().toString());
    }
    }

           本方法与第二种方法原理相同,只不过编程实现不同。可见编程魅力所在,代码简洁。但并不是所有简洁的代码都是最有效的,要理解其中的原理深入分析。


    第四种方法:


     

    public class palindromeNumber {
    public static void main(String[] args) {
    /*
    * 从控制台输入数字 调用isPalindrome() 方法判断 输出判断结果
    */
    int num;
    Scanner scan = new Scanner(System.in);
    System.out.println("请输入数字: ");
    num = scan.nextInt();
    System.out.println(isPalindrome(num));
    }

    public static boolean isPalindrome(int num) {
    boolean result = false;
    int[] arr = new int[10];
    int i = 0;
    // 将数字的每一位输入数组中
    while (num > 0) {
    arr[i] = num % 10;
    num /= 10;
    i++;
    }
    // 利用数组判断是否是回文数
    for (int j = 0; j < i / 2; j++) {
    if (arr[j] != arr[i - 1 - j]) {
    result = false;
    } else {
    result = true;
    }
    }
    return result;
    }

    }

           该方法将整数取余数,分别把每一位存入数组中,再将数组每一对应位上数值进行比较判断,最后得出结果。


           当然不仅仅可以判断数字哦,还可以试一试字符串是不是回文,比方说“你是我是你”。大家可以打开电脑,试着编一编代码哦!!!微信搜索公众号油墨山,即可获取更多资讯哦!!!

  • 相关阅读:
    Java操作Excel之POI简单例子
    机器学习之KNN算法
    机器学习之sklearn数据集
    数据分析之matplotlib
    数据分析之pandas
    数据分析之numpy
    python模块contextlib
    前端jsonp解决跨域问题
    django media和static配置
    Django之数据库迁移和创建
  • 原文地址:https://www.cnblogs.com/skylife/p/11087889.html
Copyright © 2020-2023  润新知