problem:

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

thinking:

(1)整型反转直观非常easy理解。

如正负,尾数为0等问题还优点理。

(2)反转溢出问题要细致处理。

code:

class Solution {
public:
int reverse(int x) {
long long int y=x;
bool flag=true;
if(x==0)
return 0;
if(x<0)
{
y=-x;
flag=false;
}
long long int tmp=10;
int n=1;
int m=1;
long long int result = 0;
while((y/tmp)!=0)
{
tmp*=10;
n++;
}
tmp=tmp/10;
for(int i=n;i>0;i--)
{
long long int a=y/tmp;
cout<<a<<endl;
y=y%tmp;
tmp=tmp/10;
result+=a*m;
m*=10;
}
if(abs(result)>2147483647)
return 0;
else if(!flag)
return (-result);
else
return result; }
};

leetcode题解||Reverse Integer 问题的更多相关文章

  1. LeetCode题解——Reverse Integer

    题目: 数字翻转,即输入123,返回321:输入-123,返回-321. 代码: class Solution { public: int reverse(int x) { , sign = ; ) ...

  2. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  3. [LeetCode 题解]: Reverse Nodes in K-Groups

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a li ...

  4. leetcode:Reverse Integer 及Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  5. LeetCode 7 Reverse Integer & int

    Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...

  6. Leetcode 7. Reverse Integer(水)

    7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...

  7. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  8. [LeetCode][Python]Reverse Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...

  9. LeetCode 7. Reverse Integer(c语言版)

    题目: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123Output: 321 Ex ...

随机推荐

  1. 疯狂的表单-html5新增表单元素和属性

    疯狂的表单 2015/11/27 16:44:07 从三方面来介绍html5表单的新特性 表单结构更灵活 要提交数据的控件可以布局在form标签之外,看下面的代码,表单元素可以写到form元素之外,只 ...

  2. 开发语言大PK:php和Java哪个更好?

    Java通过jdbc来访问数据库,通过不同的数据库厂商提供的数据库驱动方便地访问数据库.访问数据库的接口比较统一. PHP对于不同的数据库采用不同的数据库访问接口,所以数据库访问代码的通用性不强.例如 ...

  3. 在CAD中怎么画圆形视口的详细说明

    方法如下:在布局下画一个合适的圆,然后:命令: _-vports指定视口的角点或[开(ON)/关(OFF)/布满(F)/消隐出图(H)/锁定(L)/对象(O)/多边形(P)/恢复(R)/2/3/4]& ...

  4. php中遇到include_path='.;C:\php5\pear'的错误

    所有面页,包括空白的都会报类似下面的错误. Warning: Unknown: failed to open stream: No such file or directory in Unknown  ...

  5. FreeRTOS随记

    任务函数原型: void ATaskFunction(void * pvParameters); 任务不允许从实现函数中返回.如果一个任务不再需要,可以用vTaskDelete()删除; 一个任务函数 ...

  6. 如何在win7上面安装python的包

    最近在win7上面搞python,然后写的一些代码涉及到了对Excel的读写.所以需要用到包xlrd xlwt  xlutils. 但问题是这些包import后显示的是找不到.错误提示是:Import ...

  7. Warning: Invalid argument supplied for foreach()

    经常对提交过来的数据进行双重循环,但是为空时会报错:Warning: Invalid argument supplied for foreach() 如下解决即可:foreach($data[$i]  ...

  8. UVA 1329 - Corporative Network

    带权值的并查集的应用: 代码: #include<cstdio> #include<algorithm> #include<cmath> #include<c ...

  9. Keil工程文件的建立、设置与目标文件的获得

    单片机开发中除必要的硬件外,同样离不开软件,我们写的汇编语言源程序要变为 CPU 可以执行的机器码有两种方法,一种是手工汇编,另一种是机器汇编,目前已极少使用手工 汇编的方法了.机器汇编是通过汇编软件 ...

  10. Keil_C51程序调试过程

    调试一般都是在发生错误与意外的情况下使用的.如果程序能正常执行,调试很多时候都是用不上的.所以,最高效率的程序开发还是程序员自己做好规范,而不是指望调试来解决问题. 单片机的程序调试分为两种,一种是使 ...