已收录(2017-8-15)LeetCode算法刷题工具癖

LeetCode刷算法题 - 9. Palindrome Num

2018-04-24  本文已影响97人  蓝色小石头

写在前面:

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

上一篇下一篇

猜你喜欢

热点阅读