Leetcode算法练习-easy篇-Reverse Integer

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

Leetcode算法练习##

easy篇-Reverse Integer

最近正式入驻Leetcode,从今天开始会陆陆续续的整理在Leetcode 上的题目,用于笔记的整理与交流,而且除了自己的做法之外还会整理一些其他的好算法,并进行简单的分析与分享,考虑到算法的训练并不多,希望大家可以多多包涵。


题目要求:
Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

题目不难,但我的做法比较扭曲。。。

int reverse(int x) {
    int _[1000] = {0};
    int i = 0,j =0, k =0;
    int count = x;
    int ret = 0,sum = 1;
    if(x > 0)
    {
        for(i = 1; count >= 10; i++)
        {
            _[i] =  count % 10;
            count = count / 10;
        }
        _[i] = count;
        for(k = 1; k <= i; k++)
        {
            for(j = 0; j < i - k; j++)
            {
                sum *= 10;
            }
            ret += sum * _[k];
			sum = 1;
        }
        goto End;
    }
    if(x < 0)
    {
        count = -count;
        for(i = 1; count >= 10; i++)
        {
            _[i] =  count % 10;
            count = count / 10;
        }
        _[i] = count;
        for(k = 1; k <= i; k++)
        {
            for(j = 0; j < i - k; j++)
            {
                sum *= 10;
            }
            ret += sum * _[k];
			sum = 1;
        }
		ret = -ret;
        goto End;
    }
End:
    return ret;
}

冗余代码过多,导致程序复杂很多,导致效率很低,而且还没有考虑到整型的溢出问题,当做反面教材吧。。。


正常解法:

int reverse(int x) {
        if (x == 2147483412 )
            return 2143847412;
        if (x == -2147483412)
            return -2143847412;
        if (x > 1463847412 || x < -1463847412)
            return 0;

        int m = x/10;
        int n = x%10;
        int res = 0;
        while (m != 0) {
            res = res * 10 + n;
            n = m%10;
            m = m/10;
        }
        res = res * 10 + n;
        return res;
}

首先感谢这位同学的代码了,果真不应该按照样例的输入进行考虑啊,正向除数,之后接着反向加过了就可以了。。。


同时我也找了一下C++的做法,目前优化的最好的应该就是下面这种了,

class Solution {
public:
    int reverse(int x) {
        long long res = 0;
        while(x) {
            res = res*10 + x%10;
            x /= 10;
        }
        return (res<INT_MIN || res>INT_MAX) ? 0 : res;
    }
};

而且本题在C++和C的性能提高不大,而且因为有高级语言的特性,在C++中的溢出检查相对容易。

LeetCode刷题总结-Easy(不间断更新中) 今天你刷了吗? 英文有句谚语叫做:“An apple a day, keeps doctor away!” 换在这里就是“A problem a day, makes coding away!” 1、Two Sum 给定一个整数列表和一个目标值,列表中两个数相加为目标值,返回这两个数的下标。 解法: 暴力求解,依次遍历即可。 “哈希表”,将列表元素看做是key,将列表的下标看做是ha... 阅读详情

相关推荐

leetcode7 整数反转

给你一个 32 位的有符号整数 x ,返回 x 中每位上的数字反转后的结果。 如果反转后整数超过 32 位的有符号整数的范围[−231,231− 1] ,就返回 0。 假设环境不允许存储 64 位整数(有符号或无符号)。 示例 1: 输入:x = 123 输出:321 示例 2: 输入:x = -123 输出:-321 示例 3: 输入:x = 120 输出:21 示例 4: 输入:x = 0 输出:0 提示: -231 <= x <= 231 - 1 解题思路:本题...

qq_41295976的博客 274

NSStringEncoding关于文字编码问题的解决方法

今天看见一个很棒的博客,只是无法粉丝之,就转载一下几很好用的博文吧 转载至:http://www.cnblogs.com/zhwl/archive/2012/12/31/2840746.html PS:发现博主也是转载的,anyway,好用就行 以下为正文 今天在尝试抓取起点中文网首页的时候遇到了一个问题 — 如果编码没有用对的话是没办法读取任何东西的. 这也算是C

shang1219178163的专栏 1万+

Leetcode刷题之旅--7. 整数反转(反转有符号整数)

先来看下题目要求 开搞 一开始没考虑到溢出,解答错误。 考虑了溢出之后得到 public class Solution { public static void main(String[] args) { System.out.println(new Solution().reverse(-2147483412)); } public int reverse(int x) { int bits = 0; boolean flag =

weixin_45129136的博客 321

LeetCode—7. 反转整数

这一题真的很简单啊,不知道为什么花了好久的时间,其实功能早就实现了,题目理解错了,他是说反转后的整数溢出,则返回 0 我错误理解为输入的数据错误本来溢出就要输出0,这根本做不到嘛,因为函数中接受数据的是int类型,999999999999int他能接受,但直接是溢出后的错误结果,通过int根本无法判断输入数据是否溢出。此间,学习到了:1.存储 32 位有符号整数,其数值范围是 [−2 31,  2...

terky 283

LeetCode练习-翻转数字(Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123 Output: 321 Example 2:Input: -123 Output: -321 Example 3:Input: 120 Output: 21 Note:Assume we are dealing with an envi...

Mr_zhaoz的博客 588

leetcode java开发基础算法(Easy)

Easy 1.TwoSum(easy) /** * 利用map来存储数组中的值以及对应的index,时间复杂度为O(n),空间复杂度为O(n) * 再遍历一次map,如果map中包含有(target-key)的键,则返回这两个键的value,时间复杂度为O(n) * 总的时间复杂度为O(n),空间复杂度为O(n) * @param nums * @param target...

Bug_Solver1996的博客 440

leetcode_07_Reverse Integer (easy)

题目: Reverse Integer Total Accepted: 102355 Total Submissions: 436373 Difficulty: Easy Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 H...

dinghui1875的博客 146

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

LeetCode】 7. Reverse Integer 整数反转(Easy)(JAVA) 题目地址: https://leetcode.com/problems/reverse-integer/ 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 32...

qq_16927853的博客 178

LeetCode-Easy-007-Reverse-integer

描述 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note: Assume we are dealing wi...

你好,开发者 683

[Leetcode 7, Easy] Reverse integer

Leetcode, easy

算法的实践 629

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)

水果君の日常 3662

[LeetCode-Easy][Python] 007-Reverse Integer

题目来源 https://leetcode.com/problems/reverse-integer/description/ Descirption:Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input...

a242647980的博客 143

7. Reverse Integer [easy] (Python)

题目链接https://leetcode.com/problems/reverse-integer/题目原文 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 题目翻译反转整数中的数字。 例子1:给定x=123,返回321;例子2:给定x=-123,

Coder_Orz的博客 1万+

[LeetCode] Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Assume we are dealing with...

dongyu1703的博客 316

LeetCode easy专题】LeetCode7 Reverse Integer

       本是【leetcode easy】专题的第二,题目要求是将一个int类型的有符号数,进行逆序输出(输出也是int类型)。        可能忽略的地方有以下两点: 当输入的整数是负数或者是末尾有0的话,若采用将整数先转化为string类型,在进行输出的话,还需要单独处理负号和0的问题。 输入的int类型是32bit的,其范围为[−2^31,  2^31 − 1],其逆序...

mzqolive的博客 211

leetcode第七题(easy)——Reverse Integer

题目描述如下: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 我为这道题尝试了3种解法,一开始的解法运行时间为499ms,觉得太慢了,于是在网上参考了别人的思路,的确是快了一些,但是后来觉得在刷题的时候没这必要,运行出来就行了。三种解法的思路

zenggouzai 534

Reverse Integer - Leetcode 算法详解

7. Reverse Integer - LeetCodeeasy)Java详解

qq_16257817的博客 444

LeetCode - Easy - 7. Reverse Integer

Topic Math Description https://leetcode.com/problems/reverse-integer/ Given a 32-bit signed integer, reverse digits of an integer. Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−2³

KISS 245
下一篇: Leetcode算法练习-easy篇-Roman to Integer
晴川QZ
博客等级 码龄10年 2粉丝 10原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值