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 ...
随机推荐
- JS中对象数据类型的基本结构和操作
Object类型 ECMAScript中的队形其实就是一组数据和功能的集合.对象可以通过执行new操作符后跟要创建的对象类型的名称来创建.而创建Object类型的示例并为其添加属性和(或)方法,就可以 ...
- XMPP即时通讯协议使用(八)——基于订阅发布实现消息流转业务泳道图
- postgresql绿色版安装及Navicat创建数据库,导入导出sql
转载:https://www.cnblogs.com/winkey4986/p/5360551.html 1.设置安装路径为:D:\soft\pgsql,数据存储路径为:D:\soft\pgsql\d ...
- ubuntu 自带截图工具快捷键盘
PrtSc – 获取整个屏幕的截图并保存到 Pictures 目录. Shift + PrtSc – 获取屏幕的某个区域截图并保存到 Pictures 目录. Alt + PrtSc –获取当前窗口的 ...
- 数学相关【真·NOIP】
数论相关 上来就不会的gcd相关.见SCB他威胁我去掉了一个后缀的blog好了:https://blog.csdn.net/suncongbo/article/details/82935140(已经过 ...
- MYSQL5.7.9改密码相关设置
Centos7上,对MySQL5.7开启远程连接. 1.修改/etc/my.cnf [mysqld] validate_password=off 2.命令行进入mysql use mysql; GRA ...
- java生成快递单并调用打印机打印
生成快递单过程中需要生成条形码,生成条形码可参考:https://www.cnblogs.com/linbky/p/12091248.html 下面这段代码生成的快递单是完全符合京东快递的10 x 1 ...
- [转]Windows 10 无法访问共享的解决办法大全
本文前面介绍 Windows 10 操作系统无法访问其他电脑的共享文件夹,而其他电脑访问该共享可以访问的解决办法. 简单点说就是,你的操作系统是 Win10 ,你访问不了某台电脑的共享,但是别人可以. ...
- promise函数
一.promise函数是干什么的 promise函数是解决异步编程调用代码逻辑编写过于复杂的问题的,当网络请求非常复杂时,就会出现回调地狱,这样如果将这些代码写在一起就会看起来很复杂,且不利于阅读,如 ...
- MaxCompute studio FAQ
1. 官方文档地址 https://help.aliyun.com/document_detail/50889.html 2. Show Table Detail 中文乱码 原因是Intellij A ...