【LeetCode】C# 9、Palindrome Number

来源:互联网 发布:原生Js获取滚动条高度 编辑:程序博客网 时间:2024/06/13 10:38

Determine whether an integer is a palindrome. Do this without extra space.

判断一个整数是否符合回文规律,在不利用额外空间的前提下解决问题。
将整数化为string并首尾依次对比即可。
也可以和上题一样先把 x 翻转,后再与原 x 比较。

public class Solution {    public bool IsPalindrome(int x) {        //long y = Math.Abs((long) x);        if(x == 0) return true;        if(x<0) return false;        string snum = x.ToString();        int r = snum.Length -1;        for(int i=0;i<=(snum.Length/2);i++){            if(snum[i] != snum[r]) return false;            r--;        }        return true;    }}