题目
题目链接
https://www.nowcoder.com/practice/35b8166c135448c5a5ba2cff8d430c32?tpId=196&tqId=37087&rp=1&ru=/exam/company&qru=/exam/company&sourceUrl=%2Fexam%2Fcompany&difficulty=undefined&judgeStatus=undefined&tags=&title=
其他网站相同题目链接:
https://www.lintcode.com/problem/491
思路
数字转为字符串,双指针比较即可
参考答案Java
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param x int整型
* @return bool布尔型
*/
public boolean isPalindrome (int x) {
String s = x+"";
int len = s.length();
int L=0,R=len-1;
while(L<=R){
if(s.charAt(L) !=s.charAt(R)) return false;
L++;
R--;
}
return true;
}
}
参考答案PHP
<?php
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param x int整型
* @return bool布尔型
*/
function isPalindrome( $x )
{
$str = (string) $x;
$len = strlen($str);
$L=0;
$R= $len-1;
while($L<=$R){
if($str[$L] != $str[$R]) return false;
$L++;
$R--;
}
return true;
}
参考答案Go
package main
import "fmt"
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param x int整型
* @return bool布尔型
*/
func isPalindrome(x int) bool {
s := fmt.Sprintf("%d", x)
n := len(s)
L := 0
R := n - 1
for L <= R {
if s[L] != s[R] {
return false
}
L++
R--
}
return true
}