第一天上课,数据库老师说对于计算机系的学生,凡是在课本上学到的专业知识都是过时的。深以为然,感觉大学两年半真的不知道学了什么,为未来感到担忧,C++也不敢说是精通,入门还差不多。最近丧的不行,不管怎么样,每天还是要进步一点点。

题目:

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

感觉不难,写了才发现自己没考虑溢出的问题,为判断溢出折腾了好久,心好累,提交之后23ms,击败20%。什么时候才能击败100%呢?

class Solution {
public:
int reverse(int x) { if (x<= && x>=-)
return x;
int MAX = INT_MAX/;
int MIN = INT_MIN/;
int result = ;
while(x != ){
if ( result>MAX || result<MIN||(x > && INT_MAX-x%<result*)|| (x< && INT_MIN-x%>result*) )
return ;
result = result* + x%;
x=x/;
}
return result;
}
};

本来想把result设置成long的,但是我的编译器int和long占的字节一样,所以就只能用这样个笨方法来判断,就当是学习了。但是leetcode的编译器int和long不是占相同字节的。

C++ leetcode::Reverse Integer的更多相关文章

  1. LeetCode: Reverse Integer 解题报告

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  2. [LeetCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...

  3. [Leetcode] reverse integer 反转整数

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...

  4. LeetCode——Reverse Integer(逆置一个整数)

    问题: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321   Ha ...

  5. Leetcode: Reverse Integer 正确的思路下-要考虑代码简化

    题目: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have ...

  6. leetcode:Reverse Integer【Python版】

    1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: cl ...

  7. LeetCode——Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...

  8. [Leetcode]Reverse Integer

      核心思想:原数对10取余数赋值给新数后降一位,再把新数升一位加上下一次原数取余值,直到原数降为0. 解法如下: int reverse(int x) { bool minus = false; ) ...

  9. leetcode reverse integer&&Palindrome Number

    public class Solution { public int reverse(int x) { int ret=0; while(x!=0) { int t=x%10; ret=ret*10+ ...

随机推荐

  1. SAP S/4 HANA

    通常说到SAP,指的都是SAP Business Suite/R3(ECC) 这款产品. 那么SAP S/4 HANA与SAP R3究竟有什么不同呢? 简单地说,S/4 HANA是下一代的R/3和SA ...

  2. NYOJ 1277Decimal integer conversion (第九届河南省省赛)

    XiaoMing likes mathematics, and heis just learning how to convert numbers between different bases , ...

  3. hdu-5707-Combine String

    题意:给你三个字符串,让你计算1 2 串和3 串是否匹配,就是3串可以分解为 1  2 串,字母顺序必须是按照1 2 串的字母前后顺序. DP代码太深奥 看不太透,这个代码比较好理解一点: #incl ...

  4. vue-router两种模式的区别

    参考文献:https://blog.csdn.net/lla520/article/details/77894985/ https://segmentfault.com/a/1190000015123 ...

  5. python第二章(2)列表

    names=["zhangyang","guyun","xiangpeng","leiming","xulia ...

  6. keys(),values()和items()

    a={'a':11,'b':'bb','c':321}for x in a.items(): print(x)  # 每条都输出来print("------------")for ...

  7. 定时任务redis锁+自定义lambda优化提取冗余代码

    功能介绍: 我系统中需要跑三个定时任务,由于是多节点部署,为了防止多个节点的定时任务重复执行.所以在定时任务执行时加个锁,抢到锁的节点才能执行定时任务,没有抢到锁的节点就不执行.从而避免了定时任务重复 ...

  8. JAVA基础知识总结:十五

    一.Set接口 Set集合不允许包含相同的元素,如果试图将两个相同的元素添加到一个集合中,使用add方法,添加失败,返回false 1.HashSet HashSet是Set集合的一个实现类,大多数情 ...

  9. learn python the hard way 习题6~10总结

    习题6总结 定义字符串: 名字 = 值 其他 你也可以用 {types_of_people}的方式把它放在任何字符串中. 也就是说你可以在其他字符串中添加{},然后前面加一个 f,可用print()进 ...

  10. Python中字典和集合的用法

    本人开始学习python 希望能够慢慢的记录下去 写下来只是为了害怕自己忘记. python中的字典和其他语言一样 也是key-value的形式  利用空间换时间 可以进行快速的查找 key 是唯一的 ...