LeetCode刷算法题 - 9. Palindrome Number

写在前面:

程序=数据结构+算法,数据结构与算法的重要性就不多说了。几乎没有一个一线互联网公司招聘任何类别的技术人员是不考算法的,程序猿们都懂的,现在最权威流行的刷题平台就是 LeetCode。

LeetCode原题链接

string - C++ Reference

C++中int与string的相互转换

C++ Map常见用法说明

Question(Easy):

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

Example:

Input: 121
Output: true
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.
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?

Solution

以下代码皆是本人用 C++写的,觉得还不错的话别忘了点个赞哦。各位同学们如果有其他更高效解题思路还请不吝赐教,多多学习。

A1、反转数字判断回文数

回文数:1.负数排除 ;2.先反转数字,再比较是否与原数相等

参考LeetCode刷算法题 - 7. Reverse Integer

算法时间复杂度 O(logn),Runtime: 136 ms,代码如下

static int x=[](){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}();

class Solution {
public:
    bool isPalindrome(int x) {
        if (x<0) {
            return false;
        }
        long res = 0;
        long temp = x;
        while (temp) {
            res = res*10 + temp%10;
            temp /= 10;
        }
        return res == x;
    }
};

引用相关

LeetCode 官网

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,129评论 0 10
  • 最近正在找实习,发现自己的算法实在是不能再渣渣,在网上查了一下,发现大家都在刷leetcode的题,于是乎本渣渣也...
    caoxian阅读 4,427评论 0 2
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,348评论 0 33
  • 1. 阿宇,在事业单位上班,早九晚五。杨杨,护士,三班倒。两人是大学校友还是老乡,都是独自飘在远方求学的孩子。各种...
    aries诺诺阅读 4,198评论 1 6
  • 【day 52 菇酱】 《大家都有病》 作者是一个非常富有想法的人,用漫画的形式介绍了现当代社会存在人们心中的各...
    阿阿阿囡酱阅读 1,059评论 0 0