前话

      今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐)。从easy的开始,数一下差不多有40道,争取两个月搞定。

题目

       没想到做的第一道题目,虽然看似简单,却费了很大周折。
题目如下:

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

      刚看到这道题,首先蹦出的想法是把整数转换为字符串,然后前后位置换下再转回int型,实事证明这样是不可取的,因为当输入的int型数字很大的时候,比如412851289525983,就会产生溢出,这样在使用Integer.parseInt这种函数的时候会报错,正确的做法是对数字进行int操作,利用取10的余数获得每位数字。下面展示下,错误答案和正确答案。

代码

1.正确解
public class Solution {public int reverse(int x) {int sum = 0;while (Math.abs(x) != 0){if(Math.abs(sum) > Integer.MAX_VALUE / 10){return 0;}sum = sum * 10 + x % 10;x = x / 10;}return sum;}
}
2.错误解
public class Solution {public int reverse(int x) {if(Math.abs(x)>100){return 0;}else{String x_str=Integer.toString(x);char[] x_char = x_str.toCharArray();String x_reverse_str="";//System.out.print(Character.isDigit(x_char[0]));if(Character.isDigit(x_char[0])){             for(int i=(x_str.length()-1);i>=0;i--){x_reverse_str+=x_char[i];}                }else{x_reverse_str+=x_char[0];for(int i=(x_str.length()-1);i>=1;i--){x_reverse_str+=x_char[i];}                      }       return Integer.parseInt(x_reverse_str);}}
}
代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

【LeetCode从零单排】No.7 Reverse Integer相关推荐

  1. 【LeetCode从零单排】No.9 Palindrome Number

    题目       这道题是迄今为止最快通过的一道题,改了两次就过了,runtime一般(中等偏下,这点不太满意).Palindrome就是判断一个整数是否对称. Determine whether a ...

  2. 【LeetCode从零单排】No.8 String to Integer (丧心病狂的一道题)

    题目        题目很简单,就是写一个函数把string转换成int,但是通过率只有可怜的11%,难点是要考虑所有情况,特别是int溢出边界,反正我是写了2个小时还没解决,先放到这,有空接着搞,现 ...

  3. 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)

    1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...

  4. 【LeetCode从零单排】No15 3Sum

    题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  5. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

    题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...

  6. 【LeetCode从零单排】No 191.Number of 1 Bits(考察位运算)

    题目 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also k ...

  7. 【LeetCode从零单排】No189 .Rotate Array

    题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...

  8. 【LeetCode从零单排】No.169 Majority Element(hashmap用法)

    题目 Given an array of size n, find the majority element. The majority element is the element that app ...

  9. 【LeetCode从零单排】No118 Pascal's Triangle

    题目 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...

最新文章

  1. Python_note5 函数和代码复用+PyInstaller库+数码管绘制
  2. [linux]在使用rsync时需要注意的小细节
  3. 用shell写了一个自动编译代码的脚本
  4. 机器学习——人工神经网络之发展历史(神经元数学模型、感知器算法)
  5. J2CL –迟到总比不到好
  6. 理解GL_TRIANGLE_STRIP等绘制三角形序列的三种方式
  7. Windows环境下多个tomcat启动方法
  8. 马云:遇见好老板很重要;锤子上海法人变更;摩拜更名美团单车 | 极客头条...
  9. jq判断是否为整数_jquery怎么判断是否是数字?
  10. 简述中断处理的6个步骤_基于SCDM软件的CFD前处理工作5个步骤
  11. ubuntu16 install sogou input
  12. js排序的时间复杂度_各种排序算法时间复杂度
  13. 基于hilbert变换的数字信号_基于Hilbert变换处理绝对重力仪测量数据
  14. 成本要素****没有被分配到成本组件结构01中的成本组件
  15. c语言反步法编程,CCM模式下Boost电路的反步法非线性控制与仿真.pdf
  16. Mac Quicktime 录屏带声音
  17. pd.read_csv处理含中文的文件
  18. 缓冲流,转换流,序列化流,网络编程
  19. 电脑没有串口怎么办?
  20. VMware安装windows server2008R2x64

热门文章

  1. mysql-5.7.18-winx64 安装 net start mysql 发生系统错误2
  2. awaitUntil() 方法的使用
  3. python高通滤波器设计_python实现直方图均衡化,理想高通滤波与高斯低通滤波
  4. ios开发返回按钮消失_iOS开发之自定义导航栏返回按钮右滑返回手势失效的解决...
  5. 16进制 hbase phoenix_【建议收藏】HBase表的RowKey设计
  6. python怎么写csv文件_python怎么写csv文件
  7. 将长度为n的绳子分为m段求各段乘积的最大值
  8. android布局翻译,android – 使用翻译动画将视图从一个布局转换为另一个布局
  9. 检测是否输入字母c语言程序,C语言判断字符是否为可打印字符的方法
  10. html显示后台传来的byte类型的数据_java的数据类型