题目描述
给你一个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
There are many popular live sex cam sites that cater to all different preferences and desires. Some of the most popular ones include Chaturbate, MyFreeCams, LiveJasmin, and Flirt4Free.
Chaturbate is known for its diverse selection of cam models, ranging from amateur performers to professional porn stars. It offers a unique "tip-based" system, where viewers can tip the performers for special requests or to show their appreciation.
MyFreeCams is a popular choice for those looking for a more personalized experience, as many of the models offer private shows for a fee. It also has a large community aspect, with forums and chat rooms for viewers to interact with each other and the models.
LiveJasmin is known for its high-quality video and audio, making it a top choice for viewers who value a visually stimulating experience. It also has a wide range of categories, allowing viewers to easily find the type of performer they are looking for.
Flirt4Free is a popular site for those looking for a more intimate and interactive experience. It offers a variety of features such as cam-to-cam shows and interactive sex toys, making it a favorite among viewers who enjoy a more immersive experience.
Overall, these live sex cam sites offer a diverse range of performers and features to cater to all types of desires and preferences. Their popularity shows that the demand for live sex cams continues to grow as people seek out new and exciting forms of sexual entertainment.
https://www.metal-archives.com/users/sved141960
https://permacultureglobal.org/users/70859-anne-tripathy
https://permacultureglobal.org/users/64651-delfeayo-bellow
https://anotepad.com/notes/7jhrbaji
https://rentry.org/wgyrpnuu
Портал о строительстве https://tozak.org.ua проекты домов, ремонт, материалы и услуги для профессионалов и частных лиц.
German auto giant Mercedes https://mercedes.lewis-hamilton.cz and British racing driver Lewis Hamilton are key figures in the era of unprecedented success of Formula 1 over the last decade.
Experience passion and tradition by climbing to the top of Beaujoire https://beaujoire.nantes-fr.com home of the legendary FC Nantes. Go from finish to an unforgettable match.
Мостбет https://2021evro.ru откройте для себя азарт в казино. Простая регистрация, множество бонусов и удобный вход обеспечат незабываемый игровой опыт. Используйте промокоды для увеличения выигрышей.
The latest news https://workgid.com from the world of tourism, travel and recreation. Tourism and travel - all the most interesting on the topic
Прикольные картинки http://shutki-anekdoty.ru/kartinki-prikolnye.
Сервисный центр предлагает ремонт видеокамер rekam рядом качественый ремонт видеокамеры rekam
Скачать бесплатные приложения https://apprepository.ru и игры для Андроид. Все программы доступны на русском языке.
Скачать приложение LineBet https://linebet-download.com последнюю версию для Android телефонов в формате APK. Обзор букмекерского приложения LineBet.