leetcode-7-整数翻转
问题:

package com.example.demo;
public class Test7 {
/**
* 整数翻转 123,-123,120等数字
* 思路:
* 1、获取原始数字的%10的余数,该余数就是翻转后的最高最数
* 2、原始数/10 就是下一次要处理的数
* 3、将取余得到的数*10 + 新的余数
* (在*10时判断是否有整型越界)
*/
public int reverse(int x) {
int res = 0;
while (x != 0) {
int remainder = x % 10;
x /= 10;
// 正整数越界
if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && remainder > 7)) {
return 0;
}
// 负整数越界
if (res < Integer.MIN_VALUE / 10 || (res == Integer.MIN_VALUE / 10 && remainder < -8)) {
return 0;
}
res = res * 10 + remainder;
}
return res;
}
public static void main(String[] args) {
Test7 t = new Test7();
int reverse = t.reverse(-123);
System.out.println(reverse);
}
}
leetcode-7-整数翻转的更多相关文章
- LeetCode刷题:第七题 整数翻转 第九题 回文数
第七题题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入 ...
- 整数翻转C++实现 java实现 leetcode系列(七)
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: ...
- Leetcode 7. 整数反转(待整理)
1.题目描述 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: ...
- LeetCode:整数转罗马数字【12】
LeetCode:整数转罗马数字[12] 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 10 ...
- 前端与算法 leetcode 7. 整数反转
目录 # 前端与算法 leetcode 7. 整数反转 题目描述 概要 提示 解析 解法 算法 传入测试用例的运行结果 执行结果 GitHub仓库 # 前端与算法 leetcode 7. 整数反转 题 ...
- java实现整数翻转
** 整数翻转** 以下程序把一个整数翻转(8765变为:5678),请补充缺少的代码. int n = 8765; int m = 0; while(n>0) { m = __________ ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- [LeetCode] Flip Game 翻转游戏
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- <每日 1 OJ> -LeetCode 7. 整数反转
题目描述 给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 ...
随机推荐
- 记录混合APP开发遇到的坑!!
1.在IOS中给body绑定click事件会失效 2.在IOS中<div contenteditable="true"></div>中点击时可以弹出键盘但是 ...
- 03.Linux-CentOS系统user用户改密码问题
问题:[user@localhost ~]$ passwdChanging password for user user.Changing password for user.(current) UN ...
- java 抽象的概念 抽象类的使用
package java10; /* 抽象方法:就是加上abstract关键字,然后去掉大括号,直接分号结束 抽象类:抽象方法所在的类,必须是抽象类才行.在class之前写上abstract即可 如何 ...
- Linux ct6.5安装rabbitmq
yum install gcc glibc-devel make ncurses-devel openssl-devel xmlto 1.Erlang安装配置 下载安装包,地址http://www.e ...
- Go 查找
sort.SearchInts(a []int, b int) 从数组a中查找b,前提是a必须有序 sort.SearchFloats(a []float64, b float64) 从数组a中查找b ...
- AGC003[BCDEF]题解
2018-12-28 有点累EF明天再写叭=v= 2018-12-29 update EF B - Simplified mahjong 可以注意到 一段连续的非0序列都可以凑出 就是显然%2=0的可 ...
- python的strip和split函数
这两个函数都是string的类函数 1.strip是去掉字符串头尾的特定字符,分三个 aa=' bb=aa.rstrip(') cc=aa.lstrip(') dd=aa.strip(') print ...
- phpLite 压缩包 百度云网盘资源
链接: https://pan.baidu.com/s/1b6EnClYOznWa0OFgk4aNQg 密码: gpup
- C++ 彩色图像(RGB)三通道直方图计算和绘制,图像逆时针旋转90° 实现代码
#include "iostream" #include "opencv2/opencv.hpp" #include "vector" us ...
- jenkins的安装与使用
以前用过hudson,前段时间听以前同事说,他现在搞jenkins,zookeeper...,现在的项目 也是手动的,所以我也就搞了一个jenkins.期间也遇到好多问题,主要是自己水平不够,网上的都 ...