LeetCode刷题1

in 力扣 with 1993 comments

题目描述

给你一个32位的有符号整数x,返回将x中的数字部分反转后的结果。
如果反转后整数超过32位的有符号整数的范围 [−2^31,2^31−1],就返回 0。
假设环境不允许存储64位整数(有符号或无符号)。


思路

1.数字反转
利用%和/,将x的最后一位数字提取出来,放到返回数字rev的最前面,由于是10进制整数,所以只要每次操作时*10进一位。

int digit = x % 10;// 提取最后一位数字
x /= 10;// x去除最后一位
rev = rev*10 + digit;// rev初始值为0,赋值给最终的数字

2.整数反转的范围限制

题目要求x是32位无符号整数,环境不允许存储64位整数
反转后整数超过32位的有符号整数的范围就返回0
官方思路
-2^31 <= rev * 10 + digit <= 2^31-1
不等式不成立则返回0
然后一堆花里胡哨数学操作ToT
推导成了
(-2^31)/10 <= rev <= (2^31-1)/10
写的乱主要是不会传图,划掉

//最终代码,这里INT_MIN和INT_MAX应该是c语言中定义的int型的最大最小值
int reverse(int x) {
    int rev = 0;
    while (x != 0) {
        if (rev < INT_MIN / 10 || rev > INT_MAX / 10) {
            return 0;
        }
        int digit = x % 10;
        x /= 10;
        rev = rev * 10 + digit;
    }
    return rev;
}

我的理解
在数字转化的过程中,只要x不为0,rev就会比原来的值大10倍+,即rev = rev*10 + digit,所以为了避免超出范围,只要将rev和极值的1/10进行比较(在x!=0的前提下)。
假如还需要缩小范围,可以1、极值/100;2、条件改为while(x/10 !=0),不过该情况下对于x==0要单独考虑

今日总结

  1. 对于算法这方面还有很多需要加强的地方,主要在于做题的思路方面,需要多加练习。
  2. C、Java的基础方面还很薄弱,需要加强,多加练习
  3. 中等难度的题目就一头雾水了[o(╥﹏╥)o],我可真菜
Responses / Cancel Reply
  1. The following articles provide number activities for teenagers
    that present how you should utilize numbers for fun. Find out how
    one can make this studying expertise fun on the subsequent page.
    I was already on my husband's health insurance
    plan, however our dental and imaginative and prescient were provided
    by way of my employer, so we needed to consider the cost of buying that coverage
    on our personal or paying out of pocket.

    Reply
  2. In spite of everything, the English would no sooner give up
    the language of Shakespeare than the Spanish would forsake the tongue
    of Cervantes. In spite of everything, because of the Web's affect, language has turn out to be a mixture of emoticons and abbreviations like LOL.
    Along with the burden of your load and how bulky it is,
    components like how fast you drive and whether or not you're
    utilizing a gasoline or diesel engine will play
    a role. In a world world, wouldn't there be a benefit to talking the identical language?

    Reply
  3. I was extremely pleased to discover this site.
    I need to to thank you for your time just
    for this wonderful read!! I definitely enjoyed every little bit of it and i also
    have you book marked to look at new information in your website.

    Reply
  4. This delicacies broadly makes use of poppy seeds as well as rice in planning
    the Indian recipes. As of late, that expertise consists of Web research abilities, as nicely as good old
    school analysis techniques -- asking co-employees, making cellphone calls,
    and utilizing business reference books on the library.
    Most filters ought to be below $15 dollars,
    nonetheless, making them a a lot cheaper possibility than changing a fuel pump down the street.
    Be prepared. Carry plenty of small plastic luggage to select up waste and a sealed
    container to dispose of it.

    Reply
  5. where to buy vermox online

    Reply
  6. JamesOwexy

    https://redstoneestate.ru/

    Reply
  7. amoxicillin 100

    Reply
  8. Hi! best male ed pills great internet site.

    Reply
  9. modafinil where to get

    Reply
  10. In case you catch your canine in the act of eliminating in an inappropriate spot,
    inform him "NO!" in a agency voice, instantly take him to the place
    you do want him to go and reward him if he does.
    If a dog has been caught and corrected for eliminating in the house, she
    will study that she's not allowed to go in entrance of you, however she could not understand that you don't want her to go in the home at
    all.

    Reply