[Leetcode] 009. Palindrome number (Easy) (C++/java/python)

Source: Internet
Author: User

Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode

009.palindrome_number (Easy) links

Title: https://oj.leetcode.com/problems/palindrome-number/
Code (GitHub): Https://github.com/illuz/leetcode

Test Instructions

Determines whether a number is a palindrome number.

Analysis

Just write as you want.

    1. You can first convert to a string and then judge. (This solution can be done with Python in one sentence =w=)
    2. A better method is to directly calculate the number of palindrome and then direct comparison.
Code

C + +:(can be converted to a string first)

Class Solution {public:    bool Ispalindrome (int x) {if (x < 0) return false;int bit[10];int cnt = 0;while (x) {BIT[CN t++] = x% 10;x/= 10;} for (int i = 0; i < cnt; i++) if (bit[i]! = bit[cnt-i-1]) return False;return true;    }};


Python:

Class solution:    # @return A Boolean    def ispalindrome (self, x):        return str (x) = = str (x) [::-1]


C + +:(calculate palindrome)

Class Solution {public:    bool Ispalindrome (int x) {long Long xx = X;long long new_xx = 0;while (xx > 0) {new_xx = NE W_XX * + xx% 10;xx/= 10;} return new_xx = = (long long) x;    }};


Java:

public class Solution {public    boolean ispalindrome (int x) {        long xx = x;        Long new_xx = 0;        while (xx > 0) {            new_xx = new_xx * + xx%;            XX/= ten;        }        return new_xx = = x;    }}


Python:

Class solution:    # @return A Boolean    def ispalindrome (self, x):        xx = x        new_xx = 0 while        xx > 0:            New_xx = new_xx * + xx%            xx/=        return new_xx = = X


[Leetcode] 009. Palindrome number (Easy) (C++/java/python)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.