[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 ...
随机推荐
- shell简单脚本#1
判断/etc/inittab文件是否大于100行,如果大于,则显示”/etc/inittab is a big file.”否者显示”/etc/inittab is a small file.” #! ...
- gitlab 安装、配置
gitlab 安装.配置 对于企业级的私有 git 仓库,gitlab 是个不错的选择. 今天就来说说 gitlab 的安装.配置. 系统配置建议:最低双核 4G 内存. 当前针对 gitlab 版本 ...
- 6.纯css绘制叮当猫
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- handlebars杂记
1.{{{caption}}}三个花括号,可以解析 空格 变成 ‘空格’. 2.数据是posts:[{ }]数组时候,可以用{{posts.length}}取得其数组长度 3.handl ...
- Maven项目构建利器05——Maven的生命周期
Maven各个构建环节执行的顺序: 不能打乱顺序, 必须按照既定的正确顺序(编译,测试.打包.部署)来执行Maven的核心程序中定义了抽象的生命周期, 生命周期中各个阶段的具体任务是由插件来完成的,可 ...
- python核心编程正则表达式练习题1-2匹配由单个空格分隔的任意单词对,也就是性和名
# 匹配由单个空格分隔的任意单词对,也就是姓和名 import re patt = '[A-Za-z]+ [A-Za-z]+' # 方法一 +加号操作符匹配它左边的正则表达式至少出现一次的情况 # p ...
- Linux安装redis,启动配置不生效(指定启动加载配置文件)
一.今天有个同学问我,为什么明明安装了redis,修改了配置,启动的时候,配置还是不生效.如下图是安装后的redis文件图. 二.想加载上图中的redis.conf,进入到src中寻找到启动文件red ...
- 1 搭建K8s集群
官网:https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/#installing ...
- windows窗口启动redis
必须先得配置好环境变量,才能在窗口开启 启动服务端:redis-server 启动客户端:redis-cli
- Redis(一)
1 单机MySQL的美好时代2 Memcached(缓存)+MySQL+垂直拆分3 MySQL主从读写分离4 分库分表+水平拆分+mysql拆分5 MySQL的扩展瓶颈6 为什么使用NoSQLNoSQ ...