题目: 给定一个整数,求将该整数逆转之后的值;

举例:

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

解题思路:

在这里只用说明几个要注意的点:

1.  如果该整数是负数,-123,则逆转之后为-321,因此需要从第一位开始逆转,而不是第0位;

2.  如果整数例如:12300,则逆转后为321,而不是00321,因此逆转后,需要去除前面的0;

3.  如果整数例如:2147483647,则逆转后为7463847412 > Integer.MAX_VALUE,则返回0;

 public class Solution {
public int reverse(int x) {
char[] ch = String.valueOf(x).toCharArray();
int begin = 0;
int flag = 0; // flag用来表示是正数还是负数
int end = ch.length - 1;
if(x < 0){ // 负数
flag = 1;
}
begin = flag;
while(begin < end){
exchange(ch, begin, end);
begin ++;
end --;
}
StringBuffer sb = new StringBuffer();
int i = flag;
       // 去掉逆转后前面的0
for(; i < ch.length; i++){
if(ch[i] != '0')
break;
}
if(flag == 1)
sb.append("-");
sb.append(ch, i, ch.length - i);
if(sb.toString().equals(""))
return 0;
double res = Double.valueOf(sb.toString());
if(res > Integer.MAX_VALUE || res < Integer.MIN_VALUE)
return 0;
return Integer.valueOf(sb.toString()); }
public void exchange(char[] ch, int index1, int index2){
char c = ch[index1];
ch[index1] = ch[index2];
ch[index2] = c;
}
}

Leetcode7--->Reverse Integer(逆转整数)的更多相关文章

  1. [LintCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  2. Leetcode7 : Reverse Integer 整数反转问题

    问题描述 Example1: x = 123, return 321 Example2: x = -123, return -321 原题链接: https://leetcode.com/proble ...

  3. [leetcode]7. Reverse Integer反转整数

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

  4. [Leetcode] reverse integer 反转整数

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

  5. 7. Reverse Integer 反转整数

    [抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数).   样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...

  6. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

  7. 7. Reverse Integer[E]整数反转

    题目 Given a 32-bit signed integer, reverse digits of an integer. Example1: x = 123, return 321 Exampl ...

  8. [LeetCode] Reverse Integer 翻转整数

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

  9. LeetCode7:Reverse Integer

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

随机推荐

  1. 模态框的理解 ,jQ: loading,进度条, 省级联动 表单验证 插件

    模态框: 打开一个弹框 不关闭它就不能做框外的操作 必须关闭或弹出另外的弹框 加载延迟loading + 进度条只要有请求 就处理一下监控ajax 全局事件jquery: $('#box').ajax ...

  2. keil下JLINK在线调试仿真设置,SWD连接

    keil下JLINK在线调试仿真设置,以下三个步骤搞定: 有时我们编译时会遇到空间不足的情况,首先我们应该把 flash和RAM的size 设置为当前所用芯片的大小,如下我使用了一个片上flash 2 ...

  3. 如何在Android Studio中导入JNI生成的.so库

    由于在原来的ADT的Eclipse环境中,用ndk_build工具生成了相应的各个.so库文件之后,eclipse工具就会自动把这些库导入到apk中.而Android Studio目前为止(0.86版 ...

  4. SQLServer 2012 报表服务部署配置(1)

    由于最近客户项目中,一直在做SQL Server 方面配置.就给大家概况简述一下 报表服务安装及遇到问题.安装和运行 SQL Server 2012 的微软原厂都有最低硬件和软件要求,对于我们大多数新 ...

  5. SQL 事物回滚

    转自https://www.cnblogs.com/delphinet/archive/2010/08/17/1801424.html 第一种:   declare   @iErrorCount    ...

  6. C# 使用解析json 嵌套方法

    C#从网页不传参数 接收json数据 public String GetHtmlFromUrl(String url) { //Response.Write(url); //Response.End( ...

  7. UESTC 1307 WINDY数 (数位DP,基础)

    题意: windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数.windy想知道,在A和B之间,包括A和B,总共有多少个windy数? 思路: 就是给连续的两 ...

  8. JAVA多线程编程——JAVA内存模型

    一.何为“内存模型” 内存模型描述了程序中各个变量(实例域.静态域和数组元素)之间的关系,以及在实际计算机系统中将变量存储到内存和从内存中取出变量这样的底层细节,对象最终是存储在内存里面的,但是编译器 ...

  9. Zero to One书摘

    之所以叫书摘,是因为翻译不像翻译,书评不像书评,更像是把觉得有意义的部分摘抄下来. 第一章,未来的挑战 如何定义未来? 大部分人定义的未来都只是现在的简单延伸而已,其实技术的改变是人们无法预料的.   ...

  10. OpenGL小试牛刀第一季

    效果截图:代码展示:using System;using System.Collections.Generic;using System.ComponentModel;using System.Dat ...