LeetCode刷题1

in 力扣 with 1611 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 want to convey my passion for your kindness in support of men and women that should have assistance with this particular idea. Your personal commitment to passing the solution all-around appears to be certainly invaluable and have regularly helped many people just like me to arrive at their dreams. Your amazing interesting tips and hints entails much a person like me and a whole lot more to my office workers. Thank you; from everyone of us.

    Reply
  2. buy azithromycin without prescription neurontin pill gabapentin 800mg uk

    Reply
  3. I'm also commenting to let you know what a awesome discovery my friend's princess found checking the blog. She realized a wide variety of issues, not to mention how it is like to possess a great teaching spirit to get certain people without difficulty have an understanding of specific advanced subject matter. You truly did more than visitors' desires. Thanks for providing those invaluable, safe, revealing and fun thoughts on your topic to Julie.

    Reply
  4. I must express my passion for your generosity giving support to those people who absolutely need help with this important study. Your special commitment to passing the solution across turned out to be amazingly significant and have continually helped individuals just like me to get to their objectives. Your personal warm and friendly help can mean this much to me and even further to my office colleagues. With thanks; from all of us.

    Reply
  5. I discovered your blog web site on google and verify a few of your early posts. Proceed to keep up the very good operate. I just further up your RSS feed to my MSN Information Reader. Seeking forward to studying more from you afterward!?

    Reply
  6. buy absorica online cheap order generic zithromax 500mg azithromycin 500mg pill

    Reply
  7. My husband and i were quite ecstatic when Louis could deal with his basic research from your precious recommendations he obtained through the site. It is now and again perplexing to simply choose to be giving for free helpful hints some others could have been making money from. Therefore we acknowledge we have got the writer to appreciate for this. The main illustrations you have made, the easy blog navigation, the relationships you will assist to promote - it is most spectacular, and it's really aiding our son and us imagine that that content is amusing, which is especially important. Thanks for all!

    Reply
  8. My husband and i ended up being really more than happy Jordan managed to finish off his web research because of the precious recommendations he acquired from your own site. It is now and again perplexing just to continually be making a gift of guidelines that many some people could have been making money from. We really recognize we now have the website owner to thank for this. These explanations you made, the easy blog navigation, the friendships you help foster - it is everything exceptional, and it's letting our son and the family reckon that the matter is entertaining, which is quite indispensable. Thanks for the whole thing!

    Reply
  9. Thanks a lot for providing individuals with an exceptionally special possiblity to check tips from this site. It is always very good and also stuffed with a lot of fun for me personally and my office co-workers to visit the blog nearly 3 times every week to read through the fresh issues you will have. And lastly, I am also always impressed with your special strategies you give. Some two areas in this posting are unequivocally the finest we've ever had.

    Reply
  10. I am glad for commenting to let you know of the brilliant discovery my wife's child experienced browsing the blog. She discovered some things, not to mention what it's like to possess a marvelous giving mood to get men and women with no trouble comprehend some extremely tough subject matter. You actually did more than readers' expectations. Thanks for offering these interesting, dependable, informative not to mention cool guidance on the topic to Ethel.

    Reply