题目描述
给你一个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
I enjoy you because of your entire labor on this website. Kate takes pleasure in getting into internet research and it's really simple to grasp why. Almost all hear all regarding the lively means you make useful steps on your web blog and as well as cause participation from some other people on the subject matter so our girl is without a doubt learning a whole lot. Have fun with the remaining portion of the year. You're the one carrying out a great job.
Read reviews and was a little hesitant since I had already inputted my order. or a but thank god, I had no issues. significantly received item in a timely matter, they are in new condition. in any event so happy I made the purchase. Will be definitely be purchasing again.
cheap real jordans https://www.realjordansshoes.com/
Read reviews and was a little hesitant since I had already inputted my order. in addition but thank god, I had no issues. like received item in a timely matter, they are in new condition. direction so happy I made the purchase. Will be definitely be purchasing again.
cheap louis vuitton handbags https://www.louisvuittonsoutletonline.com/
I wish to express my gratitude for your kind-heartedness giving support to folks that have the need for guidance on your idea. Your very own commitment to passing the message throughout came to be especially helpful and has allowed guys and women just like me to attain their endeavors. Your own informative tips and hints entails this much to me and far more to my office workers. Thanks a lot; from all of us.
Read reviews and was a little hesitant since I had already inputted my order. or even but thank god, I had no issues. similar to received item in a timely matter, they are in new condition. an invaluable so happy I made the purchase. Will be definitely be purchasing again.
original louis vuittons outlet https://www.louisvuittonsoutletonline.com/
Read reviews and was a little hesitant since I had already inputted my order. or perhaps but thank god, I had no issues. particularly the received item in a timely matter, they are in new condition. direction so happy I made the purchase. Will be definitely be purchasing again.
authentic cheap jordans https://www.cheapauthenticjordans.com/
Read reviews and was a little hesitant since I had already inputted my order. otherwise but thank god, I had no issues. appreciate the received item in a timely matter, they are in new condition. anyway so happy I made the purchase. Will be definitely be purchasing again.
authentic cheap jordans https://www.realcheapretrojordanshoes.com/
order tadalafil 40mg pills cialis super active ed pills no prescription
tadalafil 40mg usa order cialis 10mg sale where can i buy ed pills
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.