leetcode题解||Reverse Integer 问题
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 问题的更多相关文章
- LeetCode题解——Reverse Integer
题目: 数字翻转,即输入123,返回321:输入-123,返回-321. 代码: class Solution { public: int reverse(int x) { , sign = ; ) ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- [LeetCode 题解]: Reverse Nodes in K-Groups
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a li ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...
- Leetcode 7. Reverse Integer(水)
7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- [LeetCode][Python]Reverse Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...
- LeetCode 7. Reverse Integer(c语言版)
题目: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123Output: 321 Ex ...
随机推荐
- iOS狂暴之路---iOS的第一个应用中能学到哪些知识
一.前文回顾 在之前已经介绍了 iOS的学习路线图,因为中间遇到一些Android开发问题,所以就耽搁了一段时间,那么接下来的这段时间我们将继续开始iOS的狂暴之路学习,按照国际惯例,第一个应用当然是 ...
- mysql的数据类型int、bigint、smallint 和 tinyint取值范围 及varchar
使用整数数据的精确数字数据类型. bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字 ...
- MAC 安装Ruby On Rails
MAC 安装Ruby On Rails 对于新入门的开发者,如何安装 Ruby, Ruby Gems 和 Rails 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 Ruby 开发 ...
- css3:user-select属性
一.user-select简介 这是在css3 UI规范中新增的一个功能,用来控制内容的可选择性 二.user-select:值 auto——默认值,用户可以选中元素中的内容 none——用户不能选择 ...
- “未能加载文件或程序集file:///E:/MoneySet.dll或它的某一个依赖项,试图加载格式不正确的程序,行203,位置5. 文件:MReportSet.resx”,
http://bbs.csdn.net/topics/390334265 1.右键卸载项目2.右键选择编辑工程文件,在打开的文件的最后一行</project>之前加以下内容: <Pr ...
- System.in.read()
用读取键盘输入必须构建 1.输入流 System.in; 2.字符输入流 InputStreamReader 3.缓存输入流 BufferedRead ...
- ifstream文件尾最后一行读两次
看下面一段代码: ifstream m_fileConfig; string str; m_fileConfig.open(FILE_OPERATORS, ios::out ...
- 转 -- MVC+EF easyui dataGrid 动态加载分页表格
首先上javascript的代码 <script type="text/javascript"> $(function () { LoadGrid(); }) //加载 ...
- STM32F072B-DISCO 深入研究 中断系统
STM32F072B-DISCO 是我认为性价比最高的一款CPU的demo系统,以前一直在用PIC的CPU但最近几年ST异军突起,几次课题查找芯片无一例外都是ST,像USB,CAN,ZIGBEE等,S ...
- asp json
<script language="JScript" runat="Server">function toObject(json) { eva ...