Palindrome Number 解题报告

Determine whether an integer is a palindrome. Do this without extra space.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

所以问题的关键要记得判断负数,程序写的相当丑。

 1 //
 2 //  main.cpp
 3 //  Longest Substring
 4 //
 5 //  Created by Bowie Hsu  on 29/11/21.
 6 //  Copyright (c) 2014年 Bowie Hsu . All rights reserved.
 7 //
 8
 9 #include <iostream>
10 #include <string>
11 #include <sstream>
12 #include <vector>
13 #include "stdio.h"
14 #include "ctype.h"
15
16 using namespace std;
17
18 class Solution
19 {
20 public:
21     bool isPalindrome(int x)
22     {
23         //记录输入整数的长度
24         if (x<0) {
25             return 0;
26         }
27         int a=1;
28         int p=0;
29         int calc=x;
30         while (calc/10!=0)
31         {
32             a=a*10;
33             calc=calc/10;
34             ++p;
35         }
36         int test=a;
37         int big=x,small=x;
38         bool bign=1;
39         int cha=0;
40         while(small)
41         {
42             if(big/test!=small%10)
43                 bign=0;
44
45             cha=big/test;
46             big=big-test*cha;
47             test=test/10;
48             small=small/10;
49
50         }
51         return bign;
52     }
53 };
54
55 int main()
56 {
57     int input=1211;
58     bool ans;
59     Solution x;
60     ans=x.isPalindrome(input);
61     cout<<ans<<endl;
62    // cout<<"what the fuck?";
63 }
时间: 2024-10-12 03:34:39

Palindrome Number 解题报告的相关文章

USACO Section1.2 Name That Number 解题报告

namenum解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 你有一个手机,键盘如下所示: 2: A,B,C 5: J,K,L 8: T,U,V 3: D,E,F 6: M,N,O 9:

ACM Minimum Inversion Number 解题报告 -线段树

C - Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.

【原创】leetCodeOj --- Largest Number 解题报告

原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may

[leetcode] 306. Additive Number 解题报告

题目链接: https://leetcode.com/problems/additive-number/ Additive number is a string whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the

Lintcode: Majority Number 解题报告

Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it. Example For [1, 1, 1, 1, 2, 2, 2], return 1 Challenge O(

LeetCode: Letter Combinations of a Phone Number 解题报告

Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23"Output

LeetCode: Valid Number 解题报告

Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambi

Letter Combinations of a Phone Number——解题报告 (回溯法的应用 )

[题目] Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"

【LeetCode】Palindrome Partitioning 解题报告

[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a","a",