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 ...
随机推荐
- 学号 20155219 《Java程序设计》第1周学习总结
学号 20155219 <Java程序设计>第1周学习总结 教材学习内容总结 JVM:是JAVA程序唯一认识的操作系统,其可执行文件为.class文档:具有让Java程序跨平台的功能.负责 ...
- 什么是 JVM
什么是 JVM 先来看下百度百科的解释: JVM 是 Java Virtual Machine(Java 虚拟机)的缩写,JVM 是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算 ...
- JavaSE笔记
this关键字 哪个对象调用方法,方法定义中的this即为该对象的引用! static关键字 使用static声名的成员变量为静态成员变量,在第一次使用的时候被初始化,static成员变量只有一份 使 ...
- selected标签判断默认选中
<select name="suggestedType" style="width:280px" > <option value=" ...
- ls -l 和du 的区别
编程之路刚刚开始,错误难免,希望大家能够指出. 简单的来说,ls -l 显示的是实际文件(目录)大小,而du显示的是文件(目录)占用磁盘空间的大小. linux下一切皆文件. 首先,硬盘的最小存储单位 ...
- Go Example--工作池
package main import ( "fmt" "time" ) func main() { jobs :=make(chan int,100) res ...
- 本地连接服务器的mongodb
首先说中心思想,和连接本地数据库是一样的,不同的是修改数据库的配置项: module.exports = { dbs:'mongodb://账号:密码@服务器ip27017/数据库名称' } 但是总是 ...
- Centos7提示swap交换空间不足解决方法
一张图就能解决的问题,就不多bb了
- smb文件共享实现
samba文件共享 首先安装软件 yum install samba -y 编辑配置文件 /etc/samba/smb.conf ,在文章最后添加以下内容 [smbtest] content = do ...
- 数据库连接池——Druid
使用步骤: 1.导包,durid1.0.9 jar包 2.定义配置文件,properties文件,名字任意位置也任意, 3.加载文件 4.获得数据库连接池对象 通过DuridDataSourceFa ...