一.题目链接: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的更多相关文章

  1. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  2. leetcode:Reverse Integer 及Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  3. LeetCode 7 Reverse Integer & int

    Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...

  4. Leetcode 7. Reverse Integer(水)

    7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...

  5. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  6. [LeetCode][Python]Reverse Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...

  7. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  8. LeetCode 7. Reverse Integer (倒转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  9. [LeetCode] 7. Reverse Integer 翻转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  10. LeetCode 7. Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...

随机推荐

  1. [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  2. const 和let的本质区别

    在let和const之间,建议优先使用const,尤其是在全局环境,不应该设置变量,只应设置常量. const优于let有几个原因.一个是const可以提醒阅读程序的人,这个变量不应该改变:另一个是c ...

  3. linux 的IP配置和网络问题的排查

    1.6  IP的配制, 首先要会用: ifconfig  和加相关参数如: ifconfig -a, 来查看,自己的电脑网络配制. 再次就必需要知道,默认IP配制文件的地方: cd /etc/sysc ...

  4. 纯js常用的代码

    1.获取表单中某属性的值 var name = document.myform.myname.value; 2.表单提交时校验,相应js代码中需要返回true或者false <form name ...

  5. 【mybatis源码学习】mybtias知识点

    Mybatis技术内幕系列博客,从原理和源码角度,介绍了其内部实现细节,无论是写的好与不好,我确实是用心写了,由于并不是介绍如何使用Mybatis的文章,所以,一些参数使用细节略掉了,我们的目标是介绍 ...

  6. Linux配置浮动IP

    在高可用集群环境中,一般都需要使用浮动IP来实现web高可用(High Availability). 浮动IP的概念以及为什么需要浮动IP请参考:https://blog.csdn.net/readi ...

  7. mysql之commit,transaction事物控制

    简单来说,transaction就是用来恢复为以前的数据. 举个例子,我想把今天输入到数据库里的数据在晚上的时候全部删除,那么我们就可以在今天早上的时候开始transaction事物,令autocom ...

  8. Electron-vue 新建Demo

    vue init simulatedgreg/electron-vue Test(项目名)

  9. 使用cmake编译luabind

    编写了一下cmakelists.txt文件 根目录 cmake_minimum_required(VERSION 2.8)project (Test) add_definitions( "/ ...

  10. jdk动态代理在idea的debug模式下不断刷新tostring方法

    在jdk的动态代理下,在使用idea进入动态代理的debug模式下,单步调试会刷新idea的tostring方法,让他自己重走了一遍代理 这个问题暂时无解