栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java判断回文字符串的方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java判断回文字符串的方法


1.将字符串反转后逐一比较

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        String str1 = "54415";
        int aaa = 5445;
        String str2 = String.valueOf(aaa);
        System.out.println(isPalindrome(str1));
        System.out.println(isPalindrome(str2));
    }

    public static boolean isPalindrome(String str) {
        StringBuilder s1 = new StringBuilder(str);
        s1.reverse();
        int n = str.length();
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (str.charAt(i) == s1.charAt(i)) {
                count++;
            }
        }
        if (count == n) {
            return true;
        } else {
            return false;
        }
    }
}
输出:
false
true


2.将字符串反转后创建新字符串直接比较

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        String str1 = "54145";
        int aaa = 544015;
        String str2 = String.valueOf(aaa);
        System.out.println(isPalindrome(str1));
        System.out.println(isPalindrome(str2));
    }

    public static boolean isPalindrome(String str) {
        StringBuilder s1 = new StringBuilder(str);
        s1.reverse();
        String s2 = new String(s1);
        if (str.equals(s2)) {
            return true;
        } else {
            return false;
        }
    }
}
输出:
true
false


3.使用截取字符串的方式比较

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        String str1 = "541045";
        int aaa = 544445;
        String str2 = String.valueOf(aaa);
        System.out.println(isPalindrome(str1));
        System.out.println(isPalindrome(str2));
    }

    public static boolean isPalindrome(String str) {
        int count = 0;
        for (int i = 0; i < str.length() / 2; i++) {
            if ((str.substring(i, i + 1)).equals(str.substring(str.length() - 1 - i, str.length() - i))) {
                count++;
            }
        }
        if (count == str.length() / 2) {
            return true;
        } else {
            return false;
        }
    }
}
输出:
false
true


4.纯数字判断回文的方法

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        int a1 = 5445;
        int a2 = 54415;
        System.out.println(isPalindrome(a1));
        System.out.println(isPalindrome(a2));
    }

    public static boolean isPalindrome(int n) {
        int temp = n;
        int m = 0;
        while (temp != 0) {
            m = m * 10 + temp % 10;
            temp /= 10;
        }
        if (m == n) {
            return true;
        } else {
            return false;
        }
    }
}
输出:
true
false
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939872.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号