#力扣:9. 回文数@FDDLC

9. 回文数 - 力扣(LeetCode)

一、Java

class Solution {
    public boolean isPalindrome(int x) {
        String s = Integer.toString(x);  // 转换成字符串
        return new StringBuilder(s).reverse().toString().equals(s);
    }
}

二、C++

#include 
#include 
using namespace std;
class Solution {
public:
    bool isPalindrome(int x) {
        string s = to_string(x);
        string t = s;  // t是一个新的字符串对象
        reverse(s.begin(),s.end());
        return s == t;
    }
};

三、Python

class Solution:
    def isPalindrome(self, x: int) -> bool:
        s = str(x)
        return s[::-1] == s

四、JavaScript

var isPalindrome = function(x) {
    s = x.toString();
    t = s.split("").reduce((acc, cur) => cur + acc, "");
    return s === t;
}

五、Go

package main

import "strconv"

func isPalindrome(x int) bool {
	s := strconv.Itoa(x)
	runes := []rune(s)
	for l, r := 0, len(runes)-1; l < r; l, r = l+1, r-1 {
		runes[l], runes[r] = runes[r], runes[l]
	}
	return string(runes) == s
}

你可能感兴趣的:(力扣刷题:Java,C++,Python,JS,Go,leetcode,算法)