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. C#中Socket用法,多个聊天和单一聊天。

    自己琢磨Socket刚刚几天,所以整理出来和大家共享一下.废话少说直接进入正题. 在C#中提供了两种网络服务,一种是Socket类,另一种是TcpListener(服务器),TcpClient(客户端 ...

  2. new Image()的用途

    new Image()用途总结: 1.图片预加载      在做游戏时,为了使图片能快打开可以做预加载.      原理:创建image对象,将image对象的src分别指向需加载的图片地址,图片被请 ...

  3. nginx服务器,php-fpm重启

    1.重启nginx服务器:首先whereis nginx找到你的nginx命令执行文件所在目录,直接/usr/local/nginx/sbin/nginx -s reload 这个路径可能每个人不一样 ...

  4. gcc编译参数-fPIC问题 `a local symbol' can not be used when making a shared object;

    gcc -shared -o hack.so hack.c/usr/bin/ld: /tmp/ccUZREwA.o: relocation R_X86_64_32 against `a local s ...

  5. 抓取天涯文章的蜘蛛代码,刚经过更新(因为天涯页面HTML代码变化)

    #_*_coding:utf-8-*- import urllib2 import traceback import codecs from BeautifulSoup import Beautifu ...

  6. nutch-1.7 编译

    转载自:http://peigang.iteye.com/blog/1563288 从nutch-.3开始 本地抓取(单击) 和 分布式抓取(集群)所使用的配置文件和命令单独分开. 资源:下载地址:h ...

  7. 小游戏 Lights Out (关灯) 的求解 —— 异或方程组

    Author : Evensgn  Blog Link : http://www.cnblogs.com/JoeFan/ Article Link : http://www.cnblogs.com/J ...

  8. iOS内存管理系列之一:对象所有权与引用计数

    当一个所有者(owner,其本身可以是任何一个Objective-C对象)做了以下某个动作时,它拥有对一个对象的所有权(ownership): 1. 创建一个对象.包括使用任何名称中包含“alloc” ...

  9. web sql Database

    http://www.ibm.com/developerworks/cn/web/1108_zhaifeng_websqldb/ http://baishanheishui.iteye.com/blo ...

  10. inline-block的垂直居中

    inline-block和inline都是不需要浮动就可以成行的,但是他们成行的效果不同. inline和浮动中的block是顶着上边,inline-block是像被一根绳子从垂直方向的中心穿过去. ...