Reverse Integer - Palindrome Number - 简单模拟

第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断。溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较。

代码如下:

 1 class Solution {
 2 public:
 3     int reverse(int x) {
 4         int stand[10]={2,1,4,7,4,8,3,6,4,8};
 5         int getnum[10];
 6         int a=0,flag=0;
 7         if(x>0)
 8             flag=1;
 9         else
10             if(x<0)
11             {
12                 flag=-1;
13                 x=-x;
14             }
15             else
16                 return 0;
17         int count=0;
18         while(x>0)
19         {
20             getnum[count]=(x%10);
21             x=x/10;
22             count++;
23         }
24
25         if(count==10)
26         {
27             int index=0;
28             for(int i=0;i<count;i++)
29             {
30                 if(stand[i]==getnum[i])
31                     index++;
32                 else
33                     if(stand[i]<getnum[i])
34                         return 0;
35                     else
36                         break;
37             }
38             if(index==10)
39             {
40                 if(flag==1)
41                     return 2147483648;
42                 else
43                     return 0;
44             }
45         }
46
47         for(int i=0;i<count;i++)
48         a=a*10+getnum[i];
49
50         return a*flag;
51     }
52 };

因为输入也一定最多是10位数,所以数组大小为10足够。

Palindrome Number

第二个题目是寻找回文数,题目要求不要使用多余的空间。不使用多余空间的方法是在将数字从低位向高位不断重组,当进行到一半的时候,必定会相等或者多出一位,除去这一位还是相等的。这样也不用担心溢出等问题。

时间: 2024-11-09 13:27:06

Reverse Integer - Palindrome Number - 简单模拟的相关文章

65. Reverse Integer &amp;&amp; Palindrome Number

Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alr

LeetCode7~9 Reverse Integer/String to Integer (atoi)/Palindrome Number

一:Reverse Integer 题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 链接:https://leetcode.com/problems/reverse-integer/ 分析:这题通过不断取余将余数存放在一个vector中,然后乘以相应的10^i次方相加即可,这里主要考虑是否overflow,因此将result设为long long int

LeetCode:Palindrome Number,Reverse Integer

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

Leetcode 题目整理-3 Palindrome Number &amp; Roman to Integer

9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the res

Palindrome Number &amp;&amp; Reverse Number

Problem I: 判断数字是不是回文字符串形式 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note th

Leetcode 9. 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

Palindrome Number (回文数)

回文数是指这样的数字:正读和倒读都是一样的.如:595,2332都是回文数,234不是回文数. 注意:负数不是回文数 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

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

HDU 1048 What Is Your Grade? (简单模拟)

 What Is Your Grade? Problem Description "Point, point, life of student!" This is a ballad(歌谣)well known in colleges, and you must care about your score in this exam too. How many points can you get? Now, I told you the rules which are used in