7. Reverse Integer

  • Total Accepted: 153147
  • Total Submissions: 644103
  • Difficulty: Easy

  Reverse digits of an integer.

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

  思路:

  本题思路很简单,有多种方法:要注意的就是判断反转之后的结果是否超出了int类型的范围。

  • 第一种是直接对10取余和除,然后每加一位,就将原先的结果乘以10后加上余数即得从最低位到当前位反转之后的结果。然后将处理后的字符串转化为long类型,判断是否超出了范围,超出则输出0,没有则直接输出结果。
  • 第二种是将数转化为String类型,判断index为0的位数是不是负号,若不是,则将整个字符串反转,若是,则将除了第0 位之后的字符串反转,然后将处理后的字符串转化为long类型,判断是否超出了范围,超出则输出0,没有则直接输出结果。

  下面的程序只是第一种方法的代码:

 public int reverse(int x) {
long res = 0 ;
while(x != 0){
res = res*10 + x%10 ;
x = x/10 ;
}
    //判断是否超出了范围
if(res > Integer.MAX_VALUE || res < Integer.MIN_VALUE){
return 0 ;
}else{
return (int)res ;
}
}

LeetCode--No.007 Reverse Integer的更多相关文章

  1. 【LeetCode】007. Reverse Integer

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  2. 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...

  3. No.007 Reverse Integer

    7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...

  4. C# 写 LeetCode easy #7 Reverse Integer

    7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...

  5. Leetcode练习题 7. Reverse Integer

    7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...

  6. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  7. [Leetcode]007. Reverse Integer

    public class Solution { public int reverse(int x) { long rev=0; while(x!=0){ rev = rev*10+x%10; x=x/ ...

  8. 【一天一道LeetCode】#7. Reverse Integer

    一天一道LeetCode系列 (一)题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, ...

  9. 【算法】LeetCode算法题-Reverse Integer

    这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...

随机推荐

  1. vue 关键词模糊查询

    页面html,绑定的列表数据为datas,关键词为 select_words,如下图 其中d.accounts和d.roleName是需要进行搜索的字段,也可以进行大小写都可以

  2. Unity之AssetBundle打包

    AssetBundle Resources:表示U3D自动将资源打成一个AssetBundle包,所有放在Resources下的文件夹都会打成一个AssetBundle包,资源非常大,Resource ...

  3. MQTT服务器本地搭建

    1.1 初认识MQTT协议. 2.1 下载压缩包 前往EMQ下载地址:http://emqtt.com/downloads ,下载您的系统的版本,一般选择稳定版. 2.2 解压并运行 C:\Users ...

  4. Eclipse常用快捷键(用到想到随时更新)

    原始链接:https://jingyan.baidu.com/article/fedf073771323235ac8977f1.html Shift+Enter在当前行的下一行插入空行(这时鼠标可以在 ...

  5. permissions required by Vibrator.vibrate: android.permission.VIBRATE

    <!-- 静止休眠 --><uses-permission android:name="android.permission.WAKE_LOCK" />&l ...

  6. kubernetes promethues预警、报警

    k8s addon中prometheus为测试事例,官方推荐生产环境使用Prometheus Operator and kube-prometheus. 1.clone 源码 git clone ht ...

  7. C# 检测证书是否安装、 安装证书

    检测是否存在指定的证书: /// <summary> /// 检测是否存在指定的证书 /// </summary> /// <returns></return ...

  8. MySQL优化(二) 优化诀窍

    一.索引的使用 (1)查询要使用索引最重要的条件是查询条件中的字段建立了索引: (2)下列几种情况可能使用到索引: <1> 对于创建的多列索引,只要查询条件使用了最坐边的列,索引一般就会被 ...

  9. java读取jar包中的文件

    随手写了一个java小工具,maven打包成功后,发现工具总是读不到打在jar包中的文件信息,要读取的文件位于 /src/main/resources 目录下,打包成功后,文件就在jar包中根目录下, ...

  10. 利用springloaded进行java class动态替换

    我们知道对于一个java文件,如Test.java,首先需要通过javac命令(javac Test.java)进行编译,生成class文件,再将class文件在jvm上进行加载运行,也就是java命 ...