问题:

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-整数翻转的更多相关文章

  1. LeetCode刷题:第七题 整数翻转 第九题 回文数

    第七题题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入 ...

  2. 整数翻转C++实现 java实现 leetcode系列(七)

    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: ...

  3. Leetcode 7. 整数反转(待整理)

    1.题目描述 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 输出: -321 示例 3: 输入: ...

  4. 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 ...

  5. 前端与算法 leetcode 7. 整数反转

    目录 # 前端与算法 leetcode 7. 整数反转 题目描述 概要 提示 解析 解法 算法 传入测试用例的运行结果 执行结果 GitHub仓库 # 前端与算法 leetcode 7. 整数反转 题 ...

  6. java实现整数翻转

    ** 整数翻转** 以下程序把一个整数翻转(8765变为:5678),请补充缺少的代码. int n = 8765; int m = 0; while(n>0) { m = __________ ...

  7. [LeetCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...

  8. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

  9. [LeetCode] Flip Game 翻转游戏

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  10. <每日 1 OJ> -LeetCode 7. 整数反转

    题目描述 给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 ...

随机推荐

  1. elasticsearch 基础 —— Query String

    使用查询解析器来解析其内容的查询.下面是一个例子: GET /_search { "query": { "query_string" : { "def ...

  2. 【记录】spring/springboot 配置mybatis打印sql

    ======================springboot mybatis 打印sql========================================== 方式 一: ##### ...

  3. rabbitmq必须应答

    当autoAck设置为true时,只要消息被消费者处理,不管成功与否,服务器都会删除该消息, 而当autoAck设置为false时,只有消息被处理,且反馈结果后才会删除 https://www.cnb ...

  4. fork/join并发编程

    Fork & Join 的具体含义 Fork 一词的原始含义是吃饭用的叉子,也有分叉的意思.在Linux 平台中,函数 fork()用来创建子进程,使得系统进程可以多一个执行分支.在 Java ...

  5. ivew 【provide/inject] 页面刷新实现reload

    1.App.vue <template> <div id="app"> <router-view v-if="isRouterAlive&q ...

  6. springBoot 连接数据库

    心得:1.先添加依赖. 2.在application.yml文件中创建mybatis的连接,与项目连接起来. 3.application.yml文件中可以自行配置服务器的端口,配置mybatis的路径 ...

  7. postman-Runner

    postman Runner配置 preview查看参数

  8. 在VMware中配置网卡之NAT模式

    为什么要在VMware中配置网卡? 因为在远程连接服务器时,需要虚拟机连接网络 虚拟机网络配置的三种模式:桥接模式,NAT模式,主机模式 NAT模式也称之为网络转换模式,两层路由: 第一层路由:物理机 ...

  9. 使用PowerShell下载必应图片

    今天想聊聊POWERSHELL对于WEB页面的一些应用,本人也是最近才发觉其实PS也是可以做爬虫的...所以想抛砖引玉给大家一个思路. 这次要用到的主要命令是 invoke-webrequest 先来 ...

  10. PHP curl_errno函数

    curl_errno — 返回最后一次的错误号 说明 int curl_errno ( resource $ch ) 返回最后一次cURL操作的错误号. 参数 ch 由 curl_init() 返回的 ...