题目描述
给你一个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要单独考虑
今日总结
- 对于算法这方面还有很多需要加强的地方,主要在于做题的思路方面,需要多加练习。
- C、Java的基础方面还很薄弱,需要加强,多加练习
- 中等难度的题目就一头雾水了[o(╥﹏╥)o],我可真菜
本文由 szr 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Sep 14, 2021 at 04:46 pm
|If you are dealing with a problem with frizzy hair, never rub it with a towel after washing it. It will damage and frizz out your hair. Instead, you should wrap it and push the towel to eliminate the moisture. When you are happy, brush and comb your hair.
I not to mention my pals came digesting the good things from your web page and so immediately I had an awful suspicion I never thanked you for those tips. All of the boys are already as a consequence warmed to study them and already have undoubtedly been loving those things. Appreciation for turning out to be really considerate and then for selecting such perfect subject areas most people are really desperate to know about. Our honest regret for not expressing gratitude to you sooner.
My husband and i ended up being quite satisfied Albert managed to finish off his preliminary research out of the ideas he received when using the site. It's not at all simplistic just to happen to be handing out steps some people have been selling. So we remember we now have the blog owner to give thanks to for this. These explanations you've made, the easy website menu, the friendships your site help to engender - it is many astonishing, and it is letting our son in addition to us imagine that that issue is amusing, and that's highly mandatory. Many thanks for the whole lot!
temovate over the counter buy clobetasol no prescription buy cordarone cheap
I not to mention my friends have been checking out the nice key points found on your web blog and then all of a sudden I had an awful suspicion I had not expressed respect to you for those tips. Most of the women ended up so excited to learn all of them and now have pretty much been using those things. Many thanks for really being considerably considerate and also for selecting this sort of nice useful guides millions of individuals are really eager to be aware of. My sincere regret for not expressing gratitude to sooner.
buy cheap benicar buy benicar sale depakote 500mg pill
Thank you for all your valuable effort on this web site. My daughter enjoys conducting investigations and it is easy to understand why. A number of us notice all regarding the powerful medium you deliver simple tactics via this website and therefore attract contribution from other individuals on this subject and my simple princess has always been being taught a lot of things. Take pleasure in the rest of the year. You are always carrying out a stunning job.
cost asacol order mesalamine 800mg generic buy irbesartan 150mg generic
I would like to show appreciation to this writer for rescuing me from this type of instance. Just after exploring throughout the world wide web and getting tips that were not productive, I thought my entire life was well over. Living minus the strategies to the problems you've resolved through the article content is a serious case, as well as those which could have in a negative way damaged my career if I hadn't discovered your web site. Your primary training and kindness in handling all things was invaluable. I am not sure what I would have done if I hadn't come across such a thing like this. I am able to now relish my future. Thanks for your time very much for this skilled and result oriented help. I will not hesitate to refer your site to anyone who ought to have guidelines about this subject.
vardenafil 20mg pills cheap plaquenil 400mg order hydroxychloroquine 200mg for sale