算法养成记:回文数判断

  • 2020 年 3 月 10 日
  • 筆記

LeetCode9

Palindrome Number

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?

进阶:

你能不将整数转为字符串来解决这个问题吗?

在实际测试里,都加了特例的情况下

执行用时分别是:

7ms,7ms,9ms,6ms

内存消耗分别是:

40.6MB,40.9MB,40.4MB,40.8MB

不加特例的情况下,前面三种的效率都不太高,都是到了12-15ms左右,甚至更多。最后一种能降到10ms以下。

最让我惊讶的是,6ms能击败100%Java版本代码。和C,C++,Python相比,真是惨。

这一版文案您还觉得满意吗?

哪里不太对,但又说不上来。

数据结构和算法一直都是程序员面试重点。写好每一个方法,每一个接口,程序的效率也会越来越高。为了学习和巩固数据结构和算法,我们特别创作了《呆萌程序员–明明凯凯算法养成记》,每天更新一篇数据结构知识点或者刷一道LeetCode题目。算法都会在LeetCode上测试。当然答案不唯一,由于能力有限,实现方法不一定是最好的,也希望各位小伙伴一起来学习分享~添加微信一起学习吧!