[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 ...
随机推荐
- Anaconda折腾记(1)
Anaconda折腾记 谨此记录小白的我在Anaconda里面的摸爬滚打 更换更新源 可以不使用命令,直接进入C盘,进入user文件夹,进入当前的用户文件夹下,记得显示隐藏文件. 找到.condarc ...
- python 压缩文件(解决压缩路径问题)
#压缩文件 def Zip_files(): datapath = filepath # 证据路径 file_newname = datapath + '.zip' # 压缩文件的名字 log.deb ...
- 我所不知的JS
几天前在阅读 MDN 文档时我发现了一些我从来不知道的 JS 特性和 API. 下面是一份简短的清单, 无论有用不有用——学习 JS 的道路似乎是没有尽头的. 标签语句 在 JS 中,你可以对 for ...
- Win10+Linux(CentOS) 双系统安装教程--踩坑实录
最近心血来潮想给自己的笔记本装一套linux系统作为开发环境, 说干就干,首先先收集一下现在linux主流版本, 貌似现在市场上应用服务器比较多的是redhat相关产品,而ubuntu的优势在于它庞大 ...
- python-函数3(全局变量与局部变量)
python-函数3(全局变量与局部变量) 全局变量与局部变量 school = "goy edu" 全局变量,在最上面定义的 def change_name(name): glo ...
- 一、Vue CLI
一.Vue CLI https://cli.vuejs.org/zh/guide/installation.html 介绍: 二.安装 # 安装 Vue Cli npm install -g @vue ...
- 22_2mybatis——CURD
1.CURD操作 第一步:创建maven工程并导入坐标 <?xml version="1.0" encoding="UTF-8"?> <pro ...
- 开发规范总结-java代码
java8新特性: 开发的时候适当用一些新特性的语法,可以使代码更简洁.譬如List根据某个属性转map.stream.函数式编程.lambda表达式 有一种场景:两个list一个转map 两个lis ...
- Gcd HDU - 6545 (基础数论)
wls 有一个整数 n,他想将 1 − n 这 n 个数字分成两组,每一组至少有一个数,并且使得两组数字的和的最大公约数最大,请输出最大的最大公约数. Input 输入一行一个整数 n. 2 ≤ n ...
- DECLARE_GLOBAL_DATA_PTR
DECLARE_GLOBAL_DATA_PTR在arch/arm/include/asm/global_data.h中定义 #include <asm-generic/global_data.h ...