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

题目:

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

Example 1:

  1. Input: 123
  2. Output: 321

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

  1. class Solution {
  2. public:
  3. int reverse(int x) {
  4.  
  5. if (x<= && x>=-)
  6. return x;
  7. int MAX = INT_MAX/;
  8. int MIN = INT_MIN/;
  9. int result = ;
  10. while(x != ){
  11. if ( result>MAX || result<MIN||(x > && INT_MAX-x%<result*)|| (x< && INT_MIN-x%>result*) )
  12. return ;
  13. result = result* + x%;
  14. x=x/;
  15. }
  16. return result;
  17. }
  18. };

本来想把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按销售订单生产和标准结算配置及操作手册

    SAP按销售订单生产和标准结算配置及操作手册 http://blog.sina.com.cn/s/blog_6787c0b80101a3tl.html SAP按销售订单生产和标准结算配置及操作手册 S ...

  2. C语言学习之桶排序

    之前的博文写了交换(冒泡)排序.选择排序,本文就写写桶排序.不过我理解的这样不算是真正上的桶排序,我的比较简单而真正的桶排序是比较复杂的,暂且就叫桶排序吧. 桶排序在排序中应该用的不多吧,个人理解的是 ...

  3. 最受欢迎的前端框架 —— Bootstrap学习

    Bootstrap是Twitter的Mark Otto和Jacob Thornton开发的,是目前最受欢迎的前端框架,它简单灵活,使得Web前端开发更加快捷方便. 首先,要基本掌握Bootstrap框 ...

  4. indexOf includes 区别

    indexOf     返回数值类型   ( 索引.-1 ) includes    返回布尔类型    ( true  .false  ),且能区分出 NaN  undefined ( 如新建空数组 ...

  5. C#给整个panel添加点击事件的方法

    首先要明白两点: panel直接添加点击事件无效 panel添加透明按钮覆盖无法实现 那么方法就是 在panel上添加pictureBox 设置 //充满整个panel pictureBox1.Doc ...

  6. spring cloud: zuul: 微网关-简单使用与路由配置

    spring cloud: zuul: 微网关-简单使用与路由配置 首先引入依赖 <dependency> <groupId>org.springframework.cloud ...

  7. Genome-wide gene-environment analyses of depression and reported lifetime traumatic experiences in UK Biobank

    Genome-wide gene-environment analyses of depression and reported lifetime traumatic experiences in U ...

  8. 掌握R语言中的apply函数族(转)

    转自:http://blog.fens.me/r-apply/ 前言 刚开始接触R语言时,会听到各种的R语言使用技巧,其中最重要的一条就是不要用循环,效率特别低,要用向量计算代替循环计算. 那么,这是 ...

  9. 框架设计——View

    [demo]: ZZSZYFP : UserControl, INavigateMdiControl(1)继承自UserControl,并实现了 INavigateMdiControl接口(2)限制表 ...

  10. BGP华为、思科选路规则

    选路规则 华为BGP选路规则 思科BGP选路规则 第0条 下一跳是否可达,如果不可达则不参与选路 BGP 向IBGP对等体发布import引入的IGP路由时, 将下一跳属性改为自身的接口地址,而非IG ...