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 ;
}
}

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

  1. LeetCode--No.007 Reverse Integer

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

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

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

  3. 【LeetCode】007. Reverse Integer

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

  4. [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/ ...

  5. 007 Reverse Integer 旋转整数

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

  6. 007. Reverse Integer

    题目链接:https://leetcode.com/problems/reverse-integer/description/ Given a 32-bit signed integer, rever ...

  7. LeetCode 【2】 Reverse Integer --007

    六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...

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

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

  9. Python字符串倒序-7. Reverse Integer

    今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...

随机推荐

  1. C基础--函数参数副本

    转自:http://blog.csdn.net/chujiangke001/article/details/38553173 void GetMemory(char *p, int num) { p ...

  2. 最小费用最大流 POJ2195-Going Home

    网络流相关知识参考: http://www.cnblogs.com/luweiseu/archive/2012/07/14/2591573.html 出处:優YoU http://blog.csdn. ...

  3. Bellman-Ford & SPFA 算法——求解单源点最短路径问题

    Bellman-Ford算法与另一个非常著名的Dijkstra算法一样,用于求解单源点最短路径问题.Bellman-ford算法除了可求解边权均非负的问题外,还可以解决存在负权边的问题(意义是什么,好 ...

  4. linux 定时器编程实例(完善中).....

    最近在写linux 下的定时器编程实验,测试发现 usleep函数在 x86 架构下的定时还是比较准确的,在arm9下 就不太准了. 今天用linux 下的setitimer()函数进行了定时 器的测 ...

  5. jquery.cookie.js存与取以及过期时间设置

    $(function(){ $(".active_out .abtn").click(function(){ $(this).parents(".active_out&q ...

  6. ARM NEON编程系列1-导论

    ARM NEON 编程系列1 - 导论 前言 本系列博文用于介绍ARM CPU下NEON指令优化. 博文github地址:github 相关代码github地址:github NEON历史 ARM处理 ...

  7. PLSQL_闪回删除FlashBack Drop表误删除如何进行恢复(案例)

    2014-06-25 Created By BaoXinjian

  8. ScrollView--嵌套GridView的解决办法

    前些日子在开发中用到了需要ScrollView嵌套GridView的情况,由于这两款控件都自带滚动条,当他们碰到一起的时候便会出问题,即GridView会显示不全. 解决办法,自定义一个GridVie ...

  9. [C语言](一)第一个Windows 32 API的窗口程序

    #include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain( ...

  10. 客户端使用自定义代理类访问WCF服务 z

    通常在客户端访问WCF服务时,都需要添加服务引用,然后在客户端app.config或 web.config文件中产生WCF服务的客户端配置信息.若是每添加一个服务都是这样做,这样势必会将比较麻烦,能否 ...