9. Palindrome Number(回文数)(leetcode)

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string?

方法一:数学方法

1、我做的时候没想到x>res这一点。这个的好处显而易见,可以减少一半的比较次数。

时间复杂度:o(n/2)                   运行时间:80ms                  占用空间:41.1 MB

方法二:栈

这个方法的运行时间比上一个大,但是占的内存小了一点。

时间复杂度:o(n/2)                 运行时间:90ms              占用空间:40.5 MB

 680. Valid Palindrome II(可删除一个数再判断是不是回文数)

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.

Example 1:

Input: "aba"
Output: True

Example 2:

Input: "abca"
Output: True
Explanation: You could delete the character 'c'.

Note:

    1. The string will only contain lowercase characters a-z. The maximum length of the string is 50000.

方法一:双指针

1、主程序里如果遇到再判断的问题记得子函数让代码更清晰。

时间复杂度:o(n)                 运行时间:17ms              占用空间:37.7 MB

苟有恒,何必三更眠五更起;最无益,莫过一日暴十日寒。
原文地址:https://www.cnblogs.com/shaer/p/10412223.html