LeetCode之9:Palindrome Number

问题描述:

LeetCode算法题之9:Palindrome Number
判断一个数字式否是回文数字,即判断它是否是对称的。原题地址

问题的陷阱与难点:

缺陷代码(79.35%)

  public boolean isPalindrome(int x) {
    if (x < 0) {
      return false;
    }
    List<Integer> list = new ArrayList<Integer>();
    while (x != 0) {
      list.add(x % 10);
      x /= 10;
    }
    int left = 0, right = list.size() - 1;
    while (left <= right) {
      if (list.get(left) == list.get(right)) {
        left++;
        right--;
      } else {
        return false;
      }
    }
    return true;
  }

只简单说下思路,将数字的每一位保存到数组中,然后从两边开始比较,并向中间逼近。其中只要出现两边不相等的数字,就直接返回false。

相关文章:

  • 2021-08-03
  • 2021-11-25
  • 2021-09-17
  • 2021-11-09
  • 2021-12-12
  • 2021-11-01
  • 2022-01-19
  • 2021-08-12
猜你喜欢
  • 2022-12-23
  • 2021-08-16
  • 2021-04-11
  • 2021-12-12
  • 2021-09-22
  • 2021-04-02
  • 2021-07-24
相关资源
相似解决方案