LeetCode刷题1

in 力扣 with 1612 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
  1. I just wanted to develop a simple message to thank you for all of the amazing pointers you are posting at this site. My extended internet lookup has at the end of the day been honored with pleasant insight to share with my friends. I would believe that most of us site visitors actually are really fortunate to live in a good network with so many special people with very helpful pointers. I feel really lucky to have come across the webpages and look forward to some more awesome moments reading here. Thanks a lot again for all the details.

    Reply
  2. cialis 40mg for sale cialis 5mg without prescription how to get ed pills without a prescription

    Reply
  3. I have to convey my admiration for your generosity giving support to all those that actually need help on this content. Your personal commitment to passing the solution along ended up being exceedingly interesting and have specifically made somebody just like me to arrive at their aims. Your entire helpful advice denotes so much to me and still more to my colleagues. Best wishes; from everyone of us.

    Reply
  4. I simply needed to thank you very much once more. I'm not certain the things that I might have created in the absence of these tips and hints documented by you about such question. It has been an absolute frightening matter in my view, nevertheless taking note of your expert manner you treated that made me to cry with delight. I'm just happy for your help and then expect you comprehend what a great job your are undertaking teaching many others with the aid of a site. I know that you have never met all of us.

    Reply
  5. I truly wanted to type a brief message in order to express gratitude to you for some of the fabulous tips and tricks you are showing at this website. My extended internet look up has finally been recognized with wonderful content to exchange with my pals. I 'd admit that we visitors are very lucky to exist in a great community with many special professionals with valuable principles. I feel very happy to have come across your entire web pages and look forward to tons of more fun times reading here. Thanks a lot again for a lot of things.

    Reply
  6. I precisely wished to thank you so much yet again. I am not sure the things I would've done without the actual recommendations shown by you directly on this situation. It had become a hard concern in my opinion, however , considering the very well-written avenue you dealt with the issue took me to jump for gladness. Now i'm thankful for this advice and hope you comprehend what a great job you are always doing teaching most people using your blog post. More than likely you have never got to know all of us.

    Reply
  7. I wanted to develop a simple remark in order to thank you for these magnificent tips you are sharing on this website. My time intensive internet research has at the end been paid with reliable strategies to talk about with my great friends. I 'd say that most of us visitors are undoubtedly blessed to dwell in a notable community with very many special professionals with interesting principles. I feel somewhat lucky to have used your entire webpage and look forward to many more excellent moments reading here. Thanks once more for everything.

    Reply
  8. I just wanted to thank you for the fast service. or it may be they look great. I received them a day earlier than expected. particularly the I will definitely continue to buy from this site. in either case I will recommend this site to my friends. Thanks!
    cheap louis vuitton online https://www.cheaplouisvuittonoutlets.com/

    Reply
  9. I just wanted to thank you for the fast service. or else they look great. I received them a day earlier than expected. which includes the I will definitely continue to buy from this site. regardless I will recommend this site to my friends. Thanks!
    cheap louis vuitton outlet https://www.cheaplouisvuittonoutletonline.com/

    Reply
  10. I just wanted to thank you for the fast service. or else they look great. I received them a day earlier than expected. similar to the I will definitely continue to buy from this site. situation I will recommend this site to my friends. Thanks!
    original louis vuittons outlet https://www.cheapreallouisvuitton.com/

    Reply