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

举例:

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. JAVA-Web04

    1 理解dom解析器机制   1)dom解析和dom4j原理一致   2)Node是所有元素的父接口   3)常用的API: DocumentBuilderFactory factory = Docu ...

  2. WPF中,DataGrid最左边多出一行的解决方案

        这种情况下,请在DataGrid的属性里加上这个属性:   RowHeaderWidth="0" 必须赋值为0,不能不赋值,也不能赋其他值. 问题解决.

  3. UVA 12898 - And Or 与和或 (思路题)

    思路就是有零一变化的位Or以后一定是1,And以后一定是0:那么如果b的二进制更长那么就把包含a的部分全部置为1或0,如果一样长那么就把不同的部分置为1或0. 今天被这题坑的地方:1默认是int,如果 ...

  4. 怎么在WEBSTORM中设置代码模板 Live Templates

    怎么在WEBSTORM中设置代码模板 Live Templates setting 里面 https://www.cnblogs.com/xinzaimengzai/p/9938464.html

  5. 2018.4.10 Ubuntu cat命令解答、用法

    cat命令是linux下的一个文本输出命令,通常是用于观看某个文件的内容的: cat主要有三大功能: 1.一次显示整个文件. $ cat filename 2.从键盘创建一个文件. $ cat > ...

  6. python_107_ __metaclass__ 元类

    类默认是由 type 类实例化产生,type类中如何实现的创建类?类又是如何创建对象? 答:类中有一个属性 __metaclass__,其用来表示该类由 谁 来实例化创建,所以,我们可以为 __met ...

  7. spring-data-JPA源码解读

    spring-data-JPA源码部分有两个很重要的部分:1.识别repositories接口 2.将接口添加代理实现类并托管spring管理 JpaRepositoriesRegistrar 目的是 ...

  8. Linux学习日记:第一天

    一,登录Linux Login:test Password:123456 Last Login:Wed Dec 3 22:40:02 on tty1 test@ubuntu: startx    进入 ...

  9. 基于Nodejs的爬虫

    简介 基于 Node.JS 爬取 博客园 1W+博文,对博文内容做关键词提取,生成词云. 演示 安装 安装 git.Node.JS.MongoDB.Yarn 克隆代码 git clone git@gi ...

  10. Java面向对象基础 ——面试题

    1面向对象基础 JAVA基础语法自行掌握. 三大特性: 一 封装:★★★★★ 概念:是指隐藏对象的属性和实现细节,仅对外提供公共访问方式. 好处:将变化隔离:便于使用:提高重用性:安全性. 封装原则: ...