LeetCode--No.007 Reverse Integer
7. Reverse Integer
- Total Accepted: 153147
- Total Submissions: 644103
- Difficulty: Easy
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
思路:
本题思路很简单,有多种方法:要注意的就是判断反转之后的结果是否超出了int类型的范围。
- 第一种是直接对10取余和除,然后每加一位,就将原先的结果乘以10后加上余数即得从最低位到当前位反转之后的结果。然后将处理后的字符串转化为long类型,判断是否超出了范围,超出则输出0,没有则直接输出结果。
- 第二种是将数转化为String类型,判断index为0的位数是不是负号,若不是,则将整个字符串反转,若是,则将除了第0 位之后的字符串反转,然后将处理后的字符串转化为long类型,判断是否超出了范围,超出则输出0,没有则直接输出结果。
下面的程序只是第一种方法的代码:
public int reverse(int x) {
long res = 0 ;
while(x != 0){
res = res*10 + x%10 ;
x = x/10 ;
}
//判断是否超出了范围
if(res > Integer.MAX_VALUE || res < Integer.MIN_VALUE){
return 0 ;
}else{
return (int)res ;
}
}
LeetCode--No.007 Reverse Integer的更多相关文章
- 【LeetCode】007. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...
- No.007 Reverse Integer
7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...
- C# 写 LeetCode easy #7 Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...
- Leetcode练习题 7. Reverse Integer
7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
- [Leetcode]007. Reverse Integer
public class Solution { public int reverse(int x) { long rev=0; while(x!=0){ rev = rev*10+x%10; x=x/ ...
- 【一天一道LeetCode】#7. Reverse Integer
一天一道LeetCode系列 (一)题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, ...
- 【算法】LeetCode算法题-Reverse Integer
这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...
随机推荐
- Java虚拟机 内存区域划分
(图片来自https://www.cnblogs.com/whgk/p/6138522.html) 先从线程私有区开始介绍 虚拟机栈 Java虚拟机栈是由一个个栈帧组成的,当一个方法被调用时,代表这个 ...
- php面试题五之nginx如何调用php和php-fpm的作用和工作原理
nginx如何调用php 采用nginx+php作为webserver的架构模式,在现如今运用相当广泛.然而第一步需要实现的是如何让nginx正确的调用php.由于nginx调用php并不是如同调用一 ...
- Django的rest_framework的权限组件和频率组件源码分析
前言: Django的rest_framework一共有三大组件,分别为认证组件:perform_authentication,权限组件:check_permissions,频率组件:check_th ...
- 激活prompt
1.下载SQLPrompt 2. 断网, 打开注册机,拷贝验证码 2. 点击activate, 拷贝代码
- Eclipse常用快捷键(用到想到随时更新)
原始链接:https://jingyan.baidu.com/article/fedf073771323235ac8977f1.html Shift+Enter在当前行的下一行插入空行(这时鼠标可以在 ...
- makefile中一些编译器选项
Libraries Static Libraries a collection of ordinary object files (目标文件的集合) loaded at program link ti ...
- Difference between MB Star C3 and MB Star C4
Many times ago, i saw a blog about MB sd connect C4 for benz, the author said he like this tool very ...
- Linux module 添加到bashrc 和临时ifort编译器 以及python2和3的配置
第一步vim ~/.bashrc按键盘的i然后source /home/export/online1/bjpara/para/modules/scripts/cn-module.sh最后:x! bas ...
- Java-Selenium,获取下拉框中的每个选项的值,并随机选择某个选项
今天逛51testing,看见有人问这个问题.现在以Select标签为例. 1.首先看页面中的下拉框,如图: 2.F12查看页面源代码,如下 <select class="form-c ...
- leveldb 学习记录(八) compact
随着运行时间的增加,memtable会慢慢 转化成 sstable. sstable会越来越多 我们就需要进行整合 compact 代码会在写入查询key值 db写入时等多出位置调用MaybeSche ...