LeetCode——7. Reverse Integer
一.题目链接:https://leetcode.com/problems/reverse-integer/
二.题目大意:
给定一个整数,要求反转该整数之后再返回;如果归返回的整数超过了int型整数的表示范围,则返回0。例如:输入123,返回321。
三.题解:
这道题目在思路上并不难,主要还是在代码的实现上,这里我主要有两种实现方式。
方法1:将数字转换成字符串,然后计算出该字符串的长度,根据长度和字符来生成一个反转后的数字。代码如下:
class Solution {
public:
int reverse(int x) { stringstream ss;//通过stringstream将int转化成string
ss<<x;
string str = ss.str();
int len = str.size();
int pos[len] = {0};
int num = -1;//根据字符的位置来决定乘以10的num次方
long temp = 0;
int flag = 0;//用于标记是否为负数
for(int i = 0; i < len; i++)
{
if(str[i] == '-')
{
flag = 1;
continue;
} else
{
num++;
temp += ((str[i] - '0') *pow(10,num));
} } if(flag == 1)
temp = -1*temp;
if(temp > (pow(2,31) -1) || temp < -pow(2,31))
temp = 0;
return temp;
}
};
此方法时间复杂度为O(n),空间复杂度为O(n)。
方法2:该方法是直接对整数x进行处理,每次利用x%10来求x的个位的数字,利用x/10来求剩余部位的数字,并利用个位数字进行迭代。代码如下:
class Solution {
public:
int reverse(int x) {
long long num = 0;
while(x)
{
num = num * 10 + x % 10;
x /= 10;
}
return (num > INT_MAX || num < INT_MIN)?0:num; }
};
这种方法的关键在于:num = num * 10 + x % 10这一步骤,实际上就是每次利用x的个位的数字当做反转后数字的高位数,此处的思想还是非常巧妙的,很值得借鉴!此方法的时间复杂度为O(n),空间复杂度为O(1)。对于此方法有几点需要注意:
1.这种方法,不用区分正负数,因为如果x为负数的话,不论是他的余数还是整除后的结果都是负数。例如-123对10取余数的话,那么结果是-3;整除10的话,结果是-12。
2.此方法,不用预先知道x的位数,每次迭代,上次的个位数就会乘以10(高上1位),这次的个位数就会变成反转后数字的个位数字;所以说这种思想(也可以认为一种技巧)一定要掌握。尤其是”它不用事先知道x的位数”这个重要的性质,以后很可能用得着。
LeetCode——7. Reverse Integer的更多相关文章
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- 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】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- LeetCode 7. Reverse Integer (倒转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- [LeetCode] 7. Reverse Integer 翻转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
随机推荐
- NOI-1.1-06-空格分隔输出-体验多个输入输出
06:空格分隔输出 总时间限制: 1000ms 内存限制: 65536kB 描述 读入一个字符,一个整数,一个单精度浮点数,一个双精度浮点数,然后按顺序输出它们,并且要求在他们之间用一个空格分隔. ...
- HDU - 5157 :Harry and magic string (回文树,求多少对不相交的回文串)
Sample Input aca aaaa Sample Output 3 15 题意: 多组输入,每次给定字符串S(|S|<1e5),求多少对不相交的回文串. 思路:可以用回文树求出以每个位置 ...
- 20155208徐子涵 2016-2017-2 《Java程序设计》第7周学习总结
20155208徐子涵 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 第十三章 时间与日期 13.1 认识时间与日期 就目前来说,即使标注为GMT(无论是文 ...
- 内存池技术(UVa 122 Tree on the level)
内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时, ...
- 小白python 安装
小白python 安装: https://blog.csdn.net/qq_36667170/article/details/79275605 https://blog.csdn.net/nmjuzi ...
- Json工具类JsonUtil
import com.alibaba.fastjson.JSONArray; import com.fasterxml.jackson.core.JsonProcessingException; im ...
- Go Example--切片
package main import ( "fmt" ) func main() { //make来初始化一个切片,必须指名切片的长度 s:= make([]string, 3) ...
- 微信公众号文章转语音tts
微信公众号里面的文章在走路或者开车时候不方便浏览,希望能增加一个文字转语音功能,那么问题来了,到底哪家文字转语音技术强呢? 经过验证,目前发现最好用的还是balabolka ,国内的什么“录音啦”,试 ...
- JDBC事务的处理-----模拟银行转账业务
定义: 数据库事务(简称:事务)是数据库管理系统执行过程中的一个逻辑单位,由一个有限的数据库操作序列构成. 概要: 一个数据库事务通常包含了一个序列的对数据库的读/写操作.它的存在包含有以下两个目的: ...
- 手动安装python库
有些时候由于工作环境的限制如proxy,不能自动安装python 的第三方的库(用python –m pip install package).在这个情况下,只能自己手动安装.以xlwt-1.3.0. ...