Reverse Integer - Palindrome Number - 简单模拟
第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断。溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较。
代码如下:
class Solution {
public:
int reverse(int x) {
int stand[]={,,,,,,,,,};
int getnum[];
int a=,flag=;
if(x>)
flag=;
else
if(x<)
{
flag=-;
x=-x;
}
else
return ;
int count=;
while(x>)
{
getnum[count]=(x%);
x=x/;
count++;
}
if(count==)
{
int index=;
for(int i=;i<count;i++)
{
if(stand[i]==getnum[i])
index++;
else
if(stand[i]<getnum[i])
return ;
else
break;
}
if(index==)
{
if(flag==)
return ;
else
return ;
}
}
for(int i=;i<count;i++)
a=a*+getnum[i];
return a*flag;
}
};
因为输入也一定最多是10位数,所以数组大小为10足够。
Palindrome Number
第二个题目是寻找回文数,题目要求不要使用多余的空间。不使用多余空间的方法是在将数字从低位向高位不断重组,当进行到一半的时候,必定会相等或者多出一位,除去这一位还是相等的。这样也不用担心溢出等问题。
Reverse Integer - Palindrome Number - 简单模拟的更多相关文章
- leetcode reverse integer&&Palindrome Number
public class Solution { public int reverse(int x) { int ret=0; while(x!=0) { int t=x%10; ret=ret*10+ ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- Leetcode 题目整理-3 Palindrome Number & Roman to Integer
9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. clic ...
- No.009 Palindrome Number
9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- LeetCode--No.009 Palindrome Number
9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...
- Q9:Palindrome Number
9. Palindrome Number 官方的链接:9. Palindrome Number Description : Determine whether an integer is a pali ...
随机推荐
- C语言2
函数是C语言的基本单位,类是java,c#,c++的基本单位 int abs(int x); double fabs(double x); 按变量的存储方式:静态变量.自动变量.寄存器变量 指针就 ...
- NET CORE 应用程序启动
原文:Application Startup作者:Steve Smith翻译:刘怡(AlexLEWIS)校对:谢炀(kiler398).许登洋(Seay) ASP.NET Core 为你的应用程序提供 ...
- php保留小数格式的多种方法
php保留小数格式,定义小数格式,小数点,位数,小数位数: 方法一:(推荐)bcmul(1000.90,1,2);//两个数相乘1000.90*1, 保留两位小数点(无四舍五入)<返回strin ...
- [转]maven入门
http://wentao365.iteye.com/blog/903396 Maven是一个采用纯Java编写的开 源项目管理工具.Maven采用了一种被称之为project object mode ...
- CATransition类动画
- (void)leftClick { [UIView beginAnimations:nil context:nil]; //display mode, slow at beginning and ...
- java 循环制作三角形
package hello; public class Sanjiao { public static void main(String[]args){ for(int i=1;i<5;i++) ...
- activity的生命周期【转】
关于activity的生命周期,越来越感觉很重要.activity的生命周期有点像asp.net中page的生命周期,经历好几个过程.重写不同的阶段,可以完成不同的功能和效果.先上一张经典的生命周期图 ...
- xcode 不值钱的动画UIButton
#import "ViewController.h" @interface ViewController () /** 按钮 */ @property(nonatomic,weak ...
- Jquery开发插件的方法
Jquery未开发插件提供了两个方法: (1)Jquery.extend(object) -为Jquery类本身添加新的方法;代码如下: $.extend({ add:function(a,b) ...
- 仿36氪(iOS版附源代码)
前言: 这是我2016年3月开始写的,利用课余时间全心投入的项目,本以为是凭着轻松愉悦的方式来学习的,中途遇到bug解决bug的时候,每天晚上几乎都是写到寝室关灯,还有一次使用Github不当写了五天 ...