【leetcode c++】09 Palindrome Number

来源:互联网 发布:苹果自制铃声软件 编辑:程序博客网 时间:2024/05/22 07:58

题目

Determine whether an integer is apalindrome. Do this without extra space.

判断回文整型。注意Dothis without extra space.

Do this without extra space.……without extra space.

 

既然如此,想来想去,一个整型最长就是10位,不多不少。我们可以用穷举法,分10种可能,写出对应的判断方式即可。

 

代码也很简单。

 

Leetcode的Accepted Solutions Runtime Distribution(截图于5月上旬)

 

源码:(VS2013)如果需要提交leetcode只需要把函数中的代码复制过去即可。

#include <iostream>using namespace std;bool ifIs(int);int main(){if (ifIs(10)) cout << " opps";return 0;}bool ifIs(int x){if (x < 0) return false;else if (x > 0 && x < 10) return true;else if (x < 100){if (x / 10 == x % 10) return true;}else if (x < 1000){if (x / 100 == x % 10) return true;}else if (x < 10000){if (    x / 1000 == x % 10 && (x / 100) % 10 == (x / 10) % 10) return true;}else if (x < 100000){if (    x / 10000 == x % 10 && (x / 1000) % 10 == (x / 10) % 10) return true;}else if (x < 1000000){if (    x / 100000 == x % 10 && (x / 10000) % 10 == (x / 10) % 10&& (x / 1000) % 10 == (x / 100) % 10) return true;}else if (x < 10000000){if (    x / 1000000 == x % 10 && (x / 100000) % 10 == (x / 10) % 10&& (x / 10000) % 10 == (x / 100) % 10) return true;}else if (x < 100000000){if (    x / 10000000 == x % 10 && (x / 1000000) % 10 == (x / 10) % 10&& (x / 100000) % 10 == (x / 100) % 10&& (x / 10000) % 10 == (x / 1000) % 10) return true;}else if (x < 1000000000){if (    x / 100000000 == x % 10 && (x / 10000000) % 10 == (x / 10) % 10&& (x / 1000000) % 10 == (x / 100) % 10&& (x / 100000) % 10 == (x / 1000) % 10) return true;}else{if (    x / 1000000000 == x % 10 && (x / 100000000) % 10 == (x / 10) % 10&& (x / 10000000) % 10 == (x / 100) % 10&& (x / 1000000) % 10 == (x / 1000) % 10&& (x / 100000) % 10 == (x / 10000) % 10) return true;}return false;}


0 0
原创粉丝点击