Reverse Integer——LeetCode进阶路⑦
原题链接https://leetcode.com/problems/reverse-integer/
- 题目描述
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321Example 2:
Input: -123
Output: -321Example 3:
Input: 120
Output: 21Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. 思路分析:从高位开始对10求余,存下来乘10,依次进行……这道题实在是太温柔~
唯一需要注意的不能用粗暴法,用数组存起来再反转,会溢出的源码附录:
class Solution {
public int reverse(int x) {
if((x<=0 && x>-10)||(x<10&&x>0)){
return x;
} long result = 0;
while(x != 0 ){
result = result*10 + x%10;
x = x/10;
} if(result <= Integer.MIN_VALUE || result >= Integer.MAX_VALUE){
return 0;
} return (int)result;
}
}
Reverse Integer——LeetCode进阶路⑦的更多相关文章
- Reverse Integer LeetCode Java
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 public cl ...
- Reverse Integer [LeetCode]
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- Reverse Integer ---- LeetCode 007
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Solution ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- LeetCode 【2】 Reverse Integer --007
六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- LeetCode之Easy篇 ——(7)Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...
- LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
- 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是除数. 唯一不同 ...
随机推荐
- Lobe Chat 数据库版安装和使用教程
DeepSeek 爆火之后官方网站一直繁忙,不少开发者都开始自己动手部署聊天客户端了. 市面上可供选择的套壳 UI 很多,但是论颜值和功能,还得是 Lobe Chat. Lobe Chat 是什么? ...
- Springboot - [05] 彩蛋~
题记部分 彩蛋一:如何更换Springboot启动时的logo (1)访问 https://www.bootschool.net/ascii-art/search,搜索到佛祖的ASCII艺术字(图)集 ...
- Elasticsearch搜索引擎学习笔记(一)
核心概念 ES -> 数据库 索引index -> 表 文档 document -> 行(记录) 字段 fields -> 列 安装Elasticsearch 1. 上传后解压 ...
- EAR_v3 《浮声三》 智能化图书管理系统
EAR_v3 <浮声三> 搭建于 Actix_Web 框架下的智能化图书管理系统 本项目的前身是 <Rusty_Borders 危墙> 的 在线控制系统 部分,经过大量开发工作 ...
- class com.ttsx.activity.item.dao.entity.RoleMenu ,Not found @TableId annotation, Cannot use Mybatis-Plus 'xxById' Method. 报错解决办法
启动项目的时候,有几条WARN警告,如图: 引起原因: 是因为数据表实体类 没有di导致的. 例如: 解决办法: 增加实体id字段!或可以忽略!不影响程序!
- AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
最近在研究AI Agent如何调用三方API,整理了一篇文章,分享给大家. 调用三方 API(Function Calling)不是通过提示词(Prompt)来实现的,而是通过函数调用机制(Funct ...
- gorm stdErr = sql: Scan error on column index 0, name "total": converting NULL to float64 is unsupported
前言 使用 gorm 查询时,报错:stdErr = sql: Scan error on column index 0, name "total": converting NUL ...
- 搭建自己的OCR服务,第三步:PPOCRLabel标注工具安装
一.安装说明 安装好了PaddleOCR后,还需要安装PPOCRLabel这个标注工具,想要自己训练模型的话,有个标注工具会起很大作用. 尤其是PPOCRLabel就是跟PaddleOCR配套的标注工 ...
- Java24你发任你发,我用Java8
大家好,我是晓凡. 各位 Java 开发者们!是不是还在为 Java 23 的新特性忙得焦头烂额? 别急,Java 24 已经悄咪咪地发布了! 这可是自 Java 21 以来的第三个非长期支持版本,而 ...
- Alice与Bob-整数分解问题脚本实现
题目: 密码学历史中,有两位知名的杰出人物,Alice和Bob.他们的爱情经过置换和轮加密也难以混淆,即使是没有身份认证也可以知根知底.就像在数学王国中的素数一样,孤傲又热情.下面是一个大整数:985 ...