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

举例:

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. 一、单例模式(Singleton)

    单例模式最初的定义出现于<设计模式>(艾迪生维斯理, 1994):“保证一个类仅有一个实例,并提供一个访问它的全局访问点.” 特点:一是某个类只能有一个实例: 二是它必须自行创建这个实例: ...

  2. 不小心踩到的XMAPP的N种问题

    1.在win10上的xampp集成环境中安装mongo扩展 按照网上搜索的下载对应文件后,在phpinfo里面还是找不到mongo的扩展信息,后面也是请教同事帮忙解决: http://www.theg ...

  3. Android 模仿苹果虚拟悬浮按钮(自动靠边、可浮现任何界面上)

    由于最近小蔡的手机音量键坏了,调节音量有点麻烦,突发奇想,想自己实现一个快捷键来调节音量.在忘上参考了一些代码,总结出一般本章,分享给大家. 首先 按钮要想实现悬浮在任何界面,那么必须是要写在服务里面 ...

  4. 设置Cookie最大存活时间

    Cookie和Session都是由Tomcat自动创建的对象,Cookie的默认最大存活时间是 -1 ,即当浏览器关闭时Cookie就消失了:Session的默认最大存活时间是30分钟. 使用Sess ...

  5. redis-----字符串操作的复杂度

    命令 含义 复杂度 set key value 设置key-value o(1) get key 获取key-value o(1) del key 删除key-value o(1) setnx set ...

  6. 看动画,秒懂人工智能&物联网

  7. 【虚拟机-磁盘管理】理解及快速测定 Azure 虚拟机的磁盘性能

    随着越来越多的用户将生产系统迁移到 Azure 平台的虚拟机服务中,Azure 虚拟机的性能愈发被关注.传统的数据中心中,我们通常使用 CPU,内存,存储和网络的性能来衡量生产压力.特别是对于 IO ...

  8. Git项目管理常用命令

    安装Git Bash后,地址:https://git-scm.com/downloads 根据自己的操作系统选择对应是安装方式 可参见码云给出的文档:http://git.mydoc.io/?t=18 ...

  9. asp页面无法访问,可尝试开始SQL Server等服务

    存在问题 asp页面的英文提示,翻译后为: "一个错误发生在服务器在处理URL.请联系系统管理员(管理人).如果您是系统管理员,请点击这里了解更多关于这个错误."   解决方案 请 ...

  10. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] A A Problem about Polyline(数学)

    题目中给出的函数具有周期性,总可以移动到第一个周期内,当然,a<b则无解. 假设移动后在上升的那段,则有a-2*x*n=b,注意限制条件x≥b,n是整数,则n≤(a-b)/(2*b).满足条件的 ...