[LeetCode]-algorithms-Reverse Integer
Reverse digits of an integer.
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer,
then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Example1: x = 123, return 321
Example2: x = -123, return -321
要求:反转整形数字,需要考虑到负数和整形数字溢出的问题
10100
=>101
1000000003
=>0
-2147483648
=>
2147483647
-2147483648
public int reverse(int x) {
StringBuilder sb = new StringBuilder();
int mod, flag=0;
boolean temp = false;
if (x==0 || x==Integer.MIN_VALUE)
return 0;
if(x<0){
x = Math.abs(x);
temp = true;
}
System.out.println(x);
while(x>0){
mod = x%10;
if(mod==0 && flag==0){}else{
sb.append(mod+"");
flag++;
}
x = x/10;
}
long val = Long.valueOf(sb.toString());
int res = (val>Integer.MAX_VALUE)? 0:(int)val;
if (temp)
res = -res;
return res;
}
[LeetCode]-algorithms-Reverse Integer的更多相关文章
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...
- Leetcode 7. Reverse Integer(水)
7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- [LeetCode][Python]Reverse Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- LeetCode 7. Reverse Integer (倒转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- [LeetCode] 7. Reverse Integer 翻转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
随机推荐
- css overflow规定元素溢出框将会发生什么
- selenium获取微博用户粉丝数
selenum的安装 selenium文档 获取微博用户粉丝数 from selenium import webdriver from time import sleep wd = webdriver ...
- 微信小程序的拖拽、缩放和旋转手势
在开发中,有时会遇到像App中的手势那样的效果,下面就仿照App实现了一下. wxml部分: <view class="touch-container"> <vi ...
- GUID在安全中作用及生成方法
参考改进于http://blog.csdn.net/jcicheng/article/details/743934 全球唯一标识符 (GUID) 是一个字母数字标识符,用于指示产品的唯一性安装.在许多 ...
- Java学习路线(完整详细版)
Java学习路线(完整详细版) https://jingyan.baidu.com/article/c1a3101e110864de656deb83.html
- shiro细节、默认的过滤器、匹配模式和顺序
部分细节 [urls] 部分的配置,其格式是:“url=拦截器[参数],拦截器[参数]”: 如果当前请求的url匹配[urls] 部分的某个url模式,将会执行其配置的拦截器. anon(anonym ...
- YUV格式详解【转】
转自:http://blog.csdn.net/searchsun/article/details/2443867 [-] YUV格式解析1播放器project2 YUV 采样 表面定义 YUV格式解 ...
- 从标准输入读取一行数组并保存(用的是字符串分割函数strtok_s() )
首先介绍字符串分割函数: char *strtok_s( char *strToken, //字符串包含一个标记或一个以上的标记. const char *strDelimit, //分隔符的设置 c ...
- 021-Zabbix4.2对IIS监控摸索记录
Zabbix是很强大,但是相关的细节技术文档貌似很少,摸索之路就显得异常难. 度娘搜了下,关于Zabbix对IIS的监控资料确实有,确实也讲如何操作了,但是细细按照对方的要求操作下,总是缺数据,no ...
- jenkins 构建时显示git分支插件、显示构建分支插件
参数化构建分支 1.安装插件:Git Parameter 2.找到我们在Jenkins中建立的工程,勾选“参数化构建过程”,并如下配置 3.在“源码管理”中如下配置 Jenkins构建完显示构建用户和 ...