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 ...
随机推荐
- Kendo UI Validator 概述
Kendo UI Validator 概述 Kendo UI Validator 支持了客戶端校驗的便捷方法,它基於 HTML 5 的表單校驗功能,支持很多內置的校驗規則,同時也提供了自定義規則的便捷 ...
- 宿主机Windows访问虚拟机Linux文件(一)
如果用户使用windows操作系统,但是在虚拟机下配置Linux内核操作操作系统,往往需要实现通过宿主机Windows操作系统访问Linux内核操作系统中资源.本次实验实现的是宿主机windows 1 ...
- java uuid第一次性能
在java中产生uuid的方式是使用java.util.UUID. UUID.randomUUID().toString(); 我在测试redis性能时,使用uuid产生测试数据,发现多线程测试red ...
- Python+selenium整合自动发邮件功能
主要实现的目的是:自动将测试报告以邮件的形式通知相关人员 from HTMLTestRunner import HTMLTestRunner import HTMLTestReport from em ...
- WinForm 窗体API移动 API阴影
窗体移动 //窗体移动API [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllI ...
- HDU 5489 Removed Interval (LIS,变形)
题意: 给出一个n个元素的序列,要求从中删除任一段长度为L的连续子序列,问删除后的LIS是多少?(n<=10w, L<=n ,元素可能为负) 思路: 如果会O(nlogn)求普通LIS的算 ...
- Ubuntu系统Apache 2部署SSL证书
几天前用Apache 2部署了一个静态网页,但通过域名访问时Google提示“不安全”,经了解,原来是缺少证书. 什么是SSL证书? SSL 是指安全套接字层,简而言之,它是一项标准技术,可确保互联网 ...
- 无旋Treap【模板】P3369
题目 详情见链接. 代码 #include<cstdio> #include<iostream> #define outd(x) printf("%d\n" ...
- VC-基础:vs2010快捷键
F12: 转到所调用过程或变量的定义 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL + SHIFT + O打开项目 CTRL + S ...
- 面向对象OONo.3单元总结
一,JML语言 1)JML理论基础:JML是一类语言,用来描述一个方法或一个类的功能.以及这个类在实现这个功能时需要的条件.可能改变的全局变量.以及由于条件问题不能实现功能时这个方法或类的行为,具有明 ...