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 ...
随机推荐
- resNet代码-小象/cv
C:\yyy\ml\dengsong\ChinaHadoop\ChinaHadoop_C4-master\ChinaHadoop_C4-master\C4_ResNet_TF http://blog. ...
- MyBatis #{} 取值注意事项
正确写法#{key} 错误写法#{key } #{}中不能加空格,不然会报错
- thinkphp error:no database select
配置正确,项目运行时确出现,no database selected . 解决方法: 需要清除 /App/Runtime runtime~.php文件
- CentOS7源码安装qbittorrent最新版本
CentOS的软件 yum 里 yum search qbittorrent yum info qbittorrent 找到的是3.37版本 官网最新的是4.12版本.但需要源码安装: 官网下载最新版 ...
- 【HDOJ2586】【Tarjan离线求LCA】
http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Others) ...
- python------模块定义、导入、优化 ------->hashlib模块
一.hashlib模块 用于加密相关的操作,3.x版本里代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法. (MD5消息摘要算法(英语 ...
- Go Example--指针
package main import ( "fmt" ) func zeroval(ival int) { ival = 0 } func zeroptr(iptr *int) ...
- Python知识点整理,基础4 - 集合操作
- Lucene&Solr(索引) 暂空
1.案例分析:什么是全文检索,如何实现全文检索 2.Lucene实现全文检索的流程 a) 创建索引 b) 查询索引 3.配置开发环境 4.创建索引库 5.查询索引库 6 ...
- 将数组A中的内容和数组B中的内容进行交换。(数组一样大)
将两个数组中的内容相互交换,必须是两个数组的内容一样大小. 思路: 结合两个整型变量之间的交换,同样可以用于内容一样大的数组.用异或关系相互交换. #include<stdio.h> in ...