关闭

leetcode: reverse-integer

标签: c++leetcode
170人阅读 评论(0) 收藏 举报
分类:

Reverse digits of an integer.

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

click to show spoilers.

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.

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?

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).


class Solution {

public:
    int reverse(int x) {
        int r = 0;
        
        for(;x;x/=10)
        {
        r = r*10+x%10;
        }
        return r;
    }
};
0
0
查看评论

《leetcode》reverse-integer

题目描述 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about t...
  • wickedvalley
  • wickedvalley
  • 2017-08-03 13:17
  • 71

4.13 leetcode -13 reverse-integer

题目描述 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about t...
  • baidu_34674626
  • baidu_34674626
  • 2017-08-28 12:58
  • 40

Leetcode在线编程 reverse-integer

Leetcode在线编程 reverse-integer 字符流,数据类型转换
  • zero_witty
  • zero_witty
  • 2016-12-11 02:52
  • 237

reverse-integer

题目描述 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about t...
  • njudongchen
  • njudongchen
  • 2017-04-03 14:56
  • 120

https://leetcode.com/problems/reverse-integer/

https://leetcode.com/problems/reverse-integer/ 这道题没难度  只需要注意reverse之后溢出  其实像123450 reverse后变成054321 只要转换成long 前面的0是不需要在意的 class Solution...
  • le_chateau
  • le_chateau
  • 2015-04-15 15:00
  • 257

reverse-integer(反转整型)

题目描述Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about thi...
  • five0918
  • five0918
  • 2016-09-26 20:32
  • 1178

LeetCode最常见的面试笔试题总结

找了一段时间的实习,总结一下LeetCode上面试出现频率比较高的题,只总结一部分,后续还会继续更新。一、Two Sum题意是给出一个数组,输出和为k的两个数。数组为无序的。 这道题的解题思路是先把数组排序,再用两个指针,分别指向头和尾,并算出头和尾的和s,再把s与k比较,如果s小于k,头指针往后...
  • u014486880
  • u014486880
  • 2016-04-26 23:55
  • 8558

LeetCode 问题汇总(算法,难度,频率)

Id Question Difficulty Frequency Data Structures Algorithms 1 Two Sum 2 5 array + set sort + two pointers ...
  • sbitswc
  • sbitswc
  • 2014-03-13 11:58
  • 12168

本博客LeetCode题解索引及各类算法问题小结汇总(C++实现)

Some explains: 1),The current problems have been solved:Welcome, EbowTang! You have solved 101 / 316 problems.(update::2016/02/15) 2),All problems are...
  • EbowTang
  • EbowTang
  • 2016-02-15 17:18
  • 5662

[leetcode]--刷leetcode算法目录和算法总结目录

这篇博文是记录所有刷leetcode算法的目录以及全部源码的github地址:所有代码的github地址:https://github.com/leetcode-hust/leetcode/tree/master/louyuting/src/leetcode目录: Fizz Buzz http://...
  • u010853261
  • u010853261
  • 2017-02-02 07:54
  • 1077
    个人资料
    • 访问:39597次
    • 积分:615
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:24篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论