Leetcode7--->Reverse Integer(逆转整数)
题目: 给定一个整数,求将该整数逆转之后的值;
举例:
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(逆转整数)的更多相关文章
- [LintCode] Reverse Integer 翻转整数
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- Leetcode7 : Reverse Integer 整数反转问题
问题描述 Example1: x = 123, return 321 Example2: x = -123, return -321 原题链接: https://leetcode.com/proble ...
- [leetcode]7. Reverse Integer反转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- [Leetcode] reverse integer 反转整数
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- 7. Reverse Integer 反转整数
[抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
- 7. Reverse Integer[E]整数反转
题目 Given a 32-bit signed integer, reverse digits of an integer. Example1: x = 123, return 321 Exampl ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- LeetCode7:Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
随机推荐
- js基础的自定义属性练习
js基础的自定义属性练习: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type ...
- 解决easyUI下拉控件无法触发onkeydown事件
实现在combotree下拉控件中按Backspace键清除combotree选中的值 下面的代码无法获取到键盘事件 <input class="easyui-combotree&qu ...
- hdfs校验和
hdfs完整性:用户希望储存和处理数据的时候,不会有任何损失或者损坏.所以提供了两种校验: 1.校验和(常用循环冗余校验CRC-32). 2.运行后台进程来检测数据块. 校验和: a.写入数据节点验证 ...
- acdream 小晴天老师系列——我有一个数列! (ST算法)
小晴天老师系列——我有一个数列! Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)S ...
- UWP开发:应用文件存储
应用设置由于数据量和数据类型的限制,有很大的局限性,所以还需要应用文件存储,以文件的方式存储数据.在每个应用的应用数据存储中,该应用拥有系统定义的根目录:一个用于本地文件,一个用于漫游文件,还有一个用 ...
- (四)SpringMVC之使用cookie实现记住密码的功能
注意:因为实现记住密码的功能需要用到json,所以需要加上这条语句: <script type="text/javascript" src="scripts/jqu ...
- noip模拟赛#14
#14: T1:f[x]=x-1(x&1)||x/2(x&1=0) 求[n,m]有多少个数可以通过变换得到k.(1e9). =>好像cf上看过类似的题,用二进制的方式来写.不过我 ...
- java基础—GUI编程(一)
一.AWT介绍
- 01_10_SERVLET如何连接Mysql数据库
01_10_SERVLET如何连接Mysql数据库 1. 实现类 public void doGet(HttpServletRequest request, HttpServletResponse r ...
- mysql 安装简介
Linux: 安装 [root @ localhost ~]# yum install mysql-server 设定为开机自动启动 [root @ localhost ~]# chkconfig m ...