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. FileStream读写文件流

    用FileStream 读取文件流并显示给文件内容 string p = @"C:\Users\Administrator\Desktop\1.txt"; FileStream f ...

  2. HTML5储存

    1.sessionStorage 特点:关闭浏览器(或标签页)后数据就不存在了.但刷新页面或使用“前进”.“后退按钮”后sessionStorage仍然存在: sessionStorage每个窗口的值 ...

  3. frameset标签代码实现网站跳转

    js代码1: document.writeln("<frameset rows=\"0, *\">"); document.writeln(&quo ...

  4. POJ 1013 Counterfeit Dollar 集合上的位运算

    Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...

  5. WebApi(二)-重新封装返回结果

    先创建要返回的结果类型: /// <summary> /// 返回类型 /// </summary> public class ApiResultModel { private ...

  6. Spring MVC源码分析(续)——请求处理

    转自:http://blog.csdn.net/shi1122/article/details/8041017 (转移位置了,时光隧道:http://www.jmatrix.org/spring/50 ...

  7. 1、C# MVC学习之NVelocity基本使用

    小白一个,刚刚开始学,大神不要笑话...... NVelocity是一个很容易上手的框架,从它开始学习,可以循序渐进 首先,创建空web应用程序,新建一般处理程序 Login2.ashx 然后,引入N ...

  8. MVC3的一个意外的异常 String was not recognized as a valid Boolean. @using (Html.BeginForm())

    客户的网站放在一个虚拟空间,之间都没有修改过程序.可是网站的后台登录页面报错  String was not recognized as a valid Boolean. ,错误指向@using (H ...

  9. python获取本地ip地址的方法

    #_*_coding:utf8_*_ #以下两种方法可以在ubuntu下或者windows下获得本地的IP地址 import socket # 方法一 localIP = socket.gethost ...

  10. BZOJ4033 T1

    Description 有一棵点数为\(N\)的树,树边有边权.给你一个在\(0-N\)之内的正整数\(K\),你要在这棵树中选择\(K\)个点,将其染成黑色,并将其他的\(N-K\)个点染成白色.将 ...