【LeetCode】Reverse Integer (2 solutions)

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

解法一:把int数转为字符数组,使用进出栈进行逆序。

class Solution
{
public:
    int reverse(int x)
    {
        int neg = 1;
        if(x < 0)
        {
            neg = -1;
            x *= -1;
        }

        stack<char> stk;

        char temp[32];
        sprintf(temp, "%d", x);
        string str;
        str = temp;

        for(string::size_type st = 0; st < str.length(); st ++)
            stk.push(str[st]);

        while(stk.size()>1 && stk.top() == ‘0‘)
            stk.pop();

        string retstr;
        while(!stk.empty())
        {
            retstr += stk.top();
            stk.pop();
        }

        return neg*atoi(retstr.c_str());
    }
};

解法二:将int数模10得到的每一位数字加入到返回值中,然后返回值乘10移位。

class Solution
{
public:
    int reverse(int x)
    {
        int neg = 1;
        if(x < 0)
        {
            x *= -1;
            neg = -1;
        }

        int ret = 0;
        while(x)
        {
            ret += x%10;
            x /= 10;
            ret *= 10;
        }

        return ret/10*neg;
    }
};

【LeetCode】Reverse Integer (2 solutions)

时间: 2024-10-25 00:21:35

【LeetCode】Reverse Integer (2 solutions)的相关文章

【LeetCode】Reverse Integer

题意: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 思路: 把整数倒转.很容易,只要先判断是否负数,存起来.之后取绝对值,把绝对值倒转后再决定是否是负数. 代码: class Solution { public: int reverse(int x) { bool neg = (x < 0); x = abs(x); int ans = 0; while(x

【leetcode】Reverse Integer(middle)☆

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出的方法 ①用数据类型转换long  或 long long ②在每次循环时先保存下数字变化之前的值,处理后单步恢复看是否相等 (比③好) ③整体恢复,看数字是否相等. 思路:注意30000这样以0结尾的数字,注意越界时返回0. 我检查越界是通过把翻转的数字再翻转回去,看是否相等. int rever

【LeetCode】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

【leetcode】Reverse Words in a String

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();

【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细. def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s [ : :  -1 ] 是将元素进行翻转 [leetcode]Reverse Words in a String (python),布布扣,bubuko.com

【LeetCode】Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

【leetcode】reverse Nodes in k-groups

问题: 给定一个链表的头指针,以及一个整数k,要求将链表按每k个为一组,组内进行链表逆置.少于k个的部分不做处理. 分析: 个人觉得问题的重点是熟悉链表的就地逆置操作,就是头插法.其他的考察点如果还有的话,就的细心程度. 实现: void reverseList(ListNode *&pre, ListNode *head) { ListNode *tail = NULL; while (head) { ListNode* next = head->next; head->next =

【LeetCode】Reverse Linked List II 解题报告

[题目] Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n

【LeetCode7】Reverse Integer★

题目描述: 解题思路: 反转的方法很简单,重点在于判断溢出的问题,下面给出了两种方法. Java代码: 方法一: 判断溢出方法:在执行完int newResult=result*10+tail语句后,紧接着进行逆运算result=(newResult-tail)/10,如果出现溢出,那么逆运算后result和newResult必然不相等,反之,如果没有溢出,则逆运算后result=newResult. 1 public class LeetCode7 { 2 public static void