Leetcode 9 回文数 java String和StringBuilder

这篇博客主要介绍了LeetCode 9题的解法,探讨如何判断一个整数是否为回文数。作者分享了两种Java实现方式:通过转字符串和翻转数字,并讨论了String与StringBuilder在字符串操作中的区别和效率问题。内容包括String的构造方法、特点以及StringBuilder的使用,强调了在字符串拼接时使用StringBuilder能避免浪费资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。

回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

例如,121 是回文,而 123 不是。

思路1:转字符串

python

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        return str(x) == str(x)[::-1]

java

public class solution9 {
    public static void main(String[] args) { //主方法
        int x = 1234321;
        solution9 sol = new solution9();
        System.out.println(sol.isPalindrome(x));//调用方法
    }

    public boolean isPalindrome(int x) {
        if(x < 0){
            return false;
        }
        String strx = "" + x;
        String newStr = new StringBuilder(""+x).reverse().toString();
        return strx.equals(newStr);
    }
}

思路2,翻转数字

python

class Solution(object):
    def isPalindrome(self, x):
        if x < 0 :
            return False

        new_x = 0 #翻转后的数
        old_x = x 
        while x > 0:
            temp = x % 10 #取最后一位数
            new_x = new_x * 10 + temp #尾数变头数
            x = x // 10  #去掉最后一位数
        return new_x == old_x

l9 = Solution()
res = l9.isPalindrome(1234321)
print(res)

java

public class solution9_2 {
    public static void main(String[] args) {
        solution9_2 sol = new solution9_2();
        boolean res = sol.isPalindrome(1234321);
        System.out.println(res);
    }
    public boolean isPalindrome(int x){
        if(x < 0){
            return false;
        }
        int new_x = 0;
        int old_x = x;
        while(x > 0){
            new_x = new_x * 10 + x % 10;
            x = x /10;
        }
        return new_x == old_x;
    }
}

****解题思路非原创,只作为笔记记录

java学习笔记

String

String构造方法:

String str = new String()  //创建一个空白字符串对象,无任何内容

String str = new String(char[] chs)  // 根据字符数组内容创建字符串

String str = new String(byte[] byt)  // 根据字节数组内容创建字符串

String str = "ABC"  //直接赋值

String对象的特点:

通过new创建的对象,每次都会new一个内存空间,即使内容相同也不是同一个对象(地址不同)

字符串方法:

==

基本类型:比较的数据值是否相同

引用类型:比较的地址值是否相同

public boolean equals()将此字符串与指定对象进行比较内容是否相同,参数直接传递一个字符串对象。
public char charAt(int index)返回指定索引处的char值
public int length()返回字符串的长度 

字符串拼接:String str = "" + 123 -> "123"

StringBuilder

对字符串进行拼接时,每次拼接都会创建一个新的对象,浪费时间和内存,可以利用StringBuilder解决此问题,StringBuilder内容可变。

public StringBuilder()  创建一个空白可变字符串对象,不含有任何内容。

public StringBuilder(String str)  根据字符串内容,创建可变字符串对象。

方法:

public StringBuilder append(任意类型)添加数据,并返回对象本身
public StringBuilder reverse() 返回相反的字符数列

String和StringBuilder相互转换:

public String toString() ------- 利用方法转换

public StringBuilder (String s) ----- 构造转换

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值