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. oracle删除用户所有表

    在删除数据表的时候往往遇到外键约束无法删除的情况,我们可以通过以下几步将数据库表删除,建议在删除库之前先对数据库进行备份,养成良好习惯. 1.删除外键 --查询用户所有表的外键,owner条件为use ...

  2. Administration Commands

    Commands useful for administrators of a hadoop cluster. balancer Runs a cluster balancing utility. A ...

  3. [BZOJ 1500] [NOI2005] 维修数列

    题目链接:BZOJ - 1500 题目分析 我要先说一下,这道题我写了一晚上,然后Debug了一整个白天..........再一次被自己的蒟蒻程度震惊= = 这道题是传说中的Splay维护数列的Bos ...

  4. Unity 截取图片并且显示出来

    Unity 截取图片并且显示出来 近期用到了unity的截图,并且把他显示出来,摸索了很多! 讲解一个东西,android可以存储一些文件在本地,比如配置文件等! 对于不同的系统,方法不一! if ( ...

  5. PHP 7.0 安装使用与性能监测!

    PHP 7.0发布,网上关于新版的介绍很多,介于 7.0 在正式发布之前已经发过若干个 Beta.8个 RC,应该不会出现重大问题.今日我将一台机器升级至 PHP 7.0 并将有关信息记录如下. 本人 ...

  6. java中如何实现全局变量

    有时一个项目中会多处涉及到路径,当你把这个项目移植到别的电脑上时就要一一修改这些路径,过程十分繁琐,所以一个全局变量在这时是必不可少的. 遗憾的是java等oo语言并没有全局变量,这怎么办呢?下面介绍 ...

  7. Keil 代码折叠功能的使用

    使用keil时将某段{......}内的代码折叠起来的方法:

  8. 一些记录查询的SQL语句

    -- ======================== 第三天 =========================== CREATE DATABASE php0408 CHARSET utf8 ;CR ...

  9. SSH自定义标签

     一.标签处理类:package cn.conris.sys.form; import java.io.IOException; import java.util.Enumeration; impor ...

  10. Java正则表达式中的捕获组的概念及相关API使用

    要弄清这三个方法,首先要弄清Java正则表达式中的捕获组的概念.捕获组也就是Pattern中以括号对“()”分割出的子Pattern.至于为什么要用捕获组呢,主要是为了能找出在一次匹配中你更关心的部分 ...