LeetCode Palindrome Number

来源:互联网 发布:mac隐藏windows分区 编辑:程序博客网 时间:2024/06/05 17:49

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

思路:就是简单的判断是不是回文数,在头尾设置两个指针i和j,判断是不是一直相等就行。本题的关键是不要有额外的空间,所以创建一个数组就是不可行的了,我是将其转换成了字符串。代码如下,已通过(leetcode)

扯淡几句:由于在机房没带耳机,没法学别的,今晚就继续更新了。

public class Solution {
   public boolean isPalindrome(int x) {
    String str=""+x;
    int i=0;
    int j=str.length()-1;
    while(i<=j) {
    if(str.charAt(i)!=str.charAt(j)) return false;
    i++;
    j--;
    }
    return true;
   }
}

0 0
原创粉丝点击