【leetcode】Reverse Integer(middle)☆

Reverse digits of an integer.

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

总结:处理整数溢出的方法

①用数据类型转换long  或 long long

②在每次循环时先保存下数字变化之前的值,处理后单步恢复看是否相等 (比③好)

③整体恢复,看数字是否相等。

思路:注意30000这样以0结尾的数字,注意越界时返回0.

我检查越界是通过把翻转的数字再翻转回去,看是否相等。

int reverse(int x) {
        int xx = abs(x);
        int ans = 0;
        int zeros = 1; //如果x = 2000 这种后面有很多0 那翻转后是2 再翻转还是2 需要乘以1000才能恢复成2000

        while(xx != 0)
        {
            int r = xx % 10;
            xx /= 10;
            ans = ans * 10 + r;
            if(ans == 0) zeros *= 10;
        }
        ans = (x < 0) ? -ans : ans;

        //检测是否溢出 思路把数字重新翻转回去,看结果是否相同
        int anss = abs(ans), check = 0;
        while(anss != 0)
        {
            int r = anss % 10;
            anss /= 10;
            check = check * 10 + r;
        }
        check = (x < 0) ? -check : check;
        check *= zeros;

        if(check != x) //溢出了
            return 0;
        else
            return ans;
    }

基于②的判断:

public int reverse(int x)
{
    int result = 0;

    while (x != 0)
    {
        int tail = x % 10;
        int newResult = result * 10 + tail;
        if ((newResult - tail) / 10 != result)
        { return 0; }
        result = newResult;
        x = x / 10;
    }

    return result;
}

基于①的判断:

int reverse(int x) {
        long num = abs((long)x);
        long new_num = 0;
        while(num) {
            new_num = new_num*10 + num%10;
            num /= 10;
        }

        if (new_num > INT_MAX) {
            return 0;
        }
        return (x<0 ? -1*new_num : new_num);
    }
时间: 2024-12-28 02:46:06

【leetcode】Reverse Integer(middle)☆的相关文章

【leetcode】Reverse Bits(middle)

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000). 思路: n一直右移, ans一直左移. class So

【leetcode】Reorder List (middle)

Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 思路: 先把链表分成两节,后半部分翻转,然后前后交叉连接. 大神的代码比我的简洁,注意分两节时用快

【leetcode】Dungeon Game (middle)

The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his wa

【leetcode】Spiral Matrix(middle)

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 思路:就按照螺旋矩阵的规律 用n记录旋转圈数 每

【leetcode】Word Break (middle)

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【leetcode】Next Permutation(middle)

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replaceme

【leetcode】House Robber (middle)

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will autom

【leetcode】3Sum Closest(middle)

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

【leetcode】Partition List(middle)

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3->2