【LeetCode】Reverse digits of an integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
java中取绝对值的函数:Math.abs(变量名)
对于溢出的判断。可以直接与Integer.MIN_VALUE和Integer.MAX_VALUE比较。也可以自己初始化min_value = 0x80000000,max_value= 0x7fffffff。
if语句尽量用条件运算语句代替,可以简化代码,使代码逻辑更加清晰。
package com.jie.easy;
import java.util.Scanner;
public class ReverseInteger {
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println("input:");
int a = sc.nextInt();
int result = reverse(a);
System.out.println("output:\n"+result);
}
public static int reverse(int x){
if(x<=Integer.MIN_VALUE || x>=Integer.MAX_VALUE)
return 0;
int res = 0;
// int flag = 1 ;
// if(x<0){
// flag = -1;
// x = -x;
// }
int flag = x < 0 ? -1 : 1;
x = Math.abs(x);
while(x>0){
res = res * 10 + x % 10;
x/=10;
}
return flag*res;
}
}
【LeetCode】Reverse digits of an integer的更多相关文章
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reverse Integer
题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 很简单 ...
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Reverse Words in a String
今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- 【LeetCode】Reverse Nodes in k-Group(k个一组翻转链表)
这是LeetCode里的第25道题. 题目要求: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最 ...
- 【leetcode】Reverse Nodes in k-Group
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
随机推荐
- c#程序中的AssemblyInfo.cs
在asp.net中有一个配置文件AssemblyInfo.cs主要用来设定生成的有关程序集的常规信息dll文件的一些参数,下面是默认的AssemblyInfo.cs文件的内容具体介绍 //是否符合公共 ...
- lintcode-51-上一个排列
51-上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 注意事项 排列中可能包含重复的整数 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其 ...
- 【Linux】Linux修改openfiles后不生效问题?
#次故障问题环境背景: Centos7.4物理机,升级过ssh和ntp: #一般只需要在此文件后面添加4行就行,配置后即可生效(exit再次登录即可生效),此次配置后没生效,reboot还是没生效,在 ...
- pta编程(1-8)
知识点:本次编程运用到的格式 #include<stdio.h> int main(void) { printf(); return 0; } 过程:1-3.没什么问题,就是注意字符的输入 ...
- Intellij IDEA 系统路径配置
在使用IDEA启动Tomcat的时候,会读取系统路径,默认路径可能不是我们想要的,可以修改 C:\MyPrograms\IntelliJ IDEA 14.0.1\bin\idea.properties ...
- Web.config配置文件中的属性add,key,value含义
这是添加自定义字符串的方式,保存是以键-值的形式保存的,可以通过key获取value,一般用这种方法配置全局内使用的字符串. <configuration>是配置文件的根配置节. < ...
- BZOJ3522 POI2014HOT-Hotels(树形dp)
分两种情况.三点两两lca相同:在三点的lca处对其统计即可,显然其离lca距离应相同:某点在另两点lca的子树外部:对每个点统计出与其距离x的点有多少个即可. 可以长链剖分做到线性,当然不会. #i ...
- Hibernate查询语言——HQL
HQL(Hibernate Query Language)查询语言是完全面向对象的查询语言,它提供了更加面向对象的封装,可以理解如多态.继承和关联. HQL的基本语法如下: select " ...
- Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)
1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a ...
- 【题解】HNOI2008GT考试
这题好难啊……完全不懂矩阵加速递推的我TAT 这道题目要求我们求出不含不吉利数字的字符串总数,那么我们有dp方程 : dp[i][j](长度为 i 的字符串,最长与不吉利数字前缀相同的后缀长度为 j ...