原题:

Reverse digits of an integer.

=>反转一个整数的数字。例子如下:

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

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

=>在做题的时候请仔细思考一下下面这些方面。

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

=>假如最后一位是0,那么结果会是什么样的呢?比如10,100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

=>你有考虑过反转后的溢出问题嘛?假如是32bit的整数,1000000003反转后就会溢出。你怎么处理这样的情况?

Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).

=>抛出一个异常?很好,假如不能抛出异常呢?其实可以重新定义这个函数(比如加一个参数)

  1. class Solution {
  2. public:
  3. int reverse(int x) {
  4. // IMPORTANT: Please reset any member data you declared, as
  5. // the same Solution instance will be reused for each test case.
  6.  
  7. };

晓东分析:

这个题目就反转本身而言是很简单的,晓东就不多分析了。所以,我主要来说一下溢出的问题,我个人的思路就是在得到反转的值的时候,先不乘上最高位,留着进行比较。

所以总的来说,还是不复杂的。

代码实现:

  1. class Solution {
  2. public:
  3. int reverse(int x) {
  4. // IMPORTANT: Please reset any member data you declared, as
  5. // the same Solution instance will be reused for each test case.
  6. int max_first_bit = 2; //default for 4
  7. int max_remain_num = 147483647;
  8.  
  9. int num = 0;
  10. int temp = abs(x);
  11.  
  12. while(temp >= 10){
  13. num = num * 10 + temp % 10;
  14. temp /= 10;
  15. }
  16.  
  17. switch(sizeof(int)){
  18. case 1:
  19. max_first_bit = 1;
  20. max_remain_num = 27;
  21. break;
  22. case 2:
  23. max_first_bit = 3;
  24. max_remain_num = 2767;
  25. break;
  26. case 4:
  27. max_first_bit = 2;
  28. max_remain_num = 147483647;
  29. break;
  30. case 8:
  31. max_first_bit = 9;
  32. max_remain_num = 223372036854775807;
  33. break;
  34. }
  35.  
  36. if(x > 0){
  37. if (temp < max_first_bit)
  38. return num * 10 + temp % 10;
  39. else if(num <= max_remain_num)
  40. return num * 10 + temp % 10;
  41. else
  42. throw x;
  43. }else{
  44. if (temp < max_first_bit)
  45. return 0 - (num * 10 + temp % 10);
  46. else if(num <= max_remain_num + 1)
  47. return 0 - (num * 10 + temp % 10);
  48. else
  49. throw x;
  50. }
  51.  
  52. }
  53. };

执行结果:

1020 / 1020 test cases passed.
Status:

Accepted

Runtime:
28 ms

希望大家有更好的算法能够提出来,不甚感谢。

若您觉得该文章对您有帮助,请在下面用鼠标轻轻按一下“顶”,哈哈~~·

Reverse Integer--整数的反转的更多相关文章

  1. 【LeetCode】7. Reverse Integer 整数反转

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...

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

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

  3. [LeetCode]7. Reverse Integer整数反转

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

  4. Leetcode7 : Reverse Integer 整数反转问题

    问题描述 Example1: x = 123, return 321 Example2: x = -123, return -321 原题链接: https://leetcode.com/proble ...

  5. 【LeetCode】7. Reverse Integer 整型数反转

    题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 思路:不 ...

  6. 7. Reverse Integer (整数的溢出)

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

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

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

  8. leetcode 7 reverse integer 反转整数

    描述: 给定32位整数,反转,如321转成123. 解决: 关键是溢出检测: int reverse(int x) { ; int temp; while (x) { temp = ret * + x ...

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

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

  10. Reverse Integer - 反转一个int,溢出时返回0

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

随机推荐

  1. lr_start_transaction/lr_end_transaction事物组合

    lr_start_transaction/lr_end_transaction事物组合 总结一下: lr_start_transaction与lr_end_transaction 为使用最多的事物创造 ...

  2. bzoj 1880 最短路径图

    #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...

  3. 【笔试题】Java 继承知识点检测

    笔试题 Java 继承知识点检测 Question 1 Output of following Java Program? class Base { public void show() { Syst ...

  4. img标签src图片地址找不到显示默认图片

    可以采用onerror的属性: onerror="javascript:this.src='${base}/after/img/aifu.png'" <img id=&quo ...

  5. c#多线程实现函数同步运行

    我们假设有方法run1()和run2(),耗时都比较大,实现他们同步运行将大大提高程序的效率,在这里考虑使用多线程的方法. 首先添加引用,定义bool型i,j为false. using System. ...

  6. HDU 6026 Deleting Edges

    最短路. 先建一个只包含最短路的有向无环图,每一个点选择任意一条入边即可生成一个树形图,那么树的种类就等于每个点的入度乘积. #include <bits/stdc++.h> using ...

  7. platform 模块

    python中,platform 模块给我们提供了很多方法去获取操作系统的信息,如: import platform platform.platform() #获取操作系统名称及版本号 'Window ...

  8. shell kill session

    ps -ef | grep java kill -9 pid

  9. Kail Linux渗透测试教程之Recon-NG框架

    Kail Linux渗透测试教程之Recon-NG框架 信息收集 信息收集是网络攻击最重要的阶段之一.要想进行渗透攻击,就需要收集目标的各类信息.收集到的信息越多,攻击成功的概率也就越大.本章将介绍信 ...

  10. 更改paramiko 源码 记录命令实现堡垒机功能

    利用paramiko 下的demo可以很容易的实现记录客户在操作客户机时的命令,修改\demos\interactive.py def posix_shell(chan): import select ...