Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

Have you thought about this?

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的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【leetcode】Reverse Integer

    题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 很简单 ...

  3. 【LeetCode】Reverse Integer(整数反转)

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

  4. 【LeetCode】Add Digits

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  5. 【leetcode】Reverse Bits(middle)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  6. 【leetcode】Reverse Words in a String

    今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...

  7. 【LeetCode】Reverse Words in a String 反转字符串中的单词

    一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...

  8. 【LeetCode】Reverse Nodes in k-Group(k个一组翻转链表)

    这是LeetCode里的第25道题. 题目要求: 给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最 ...

  9. 【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 ...

随机推荐

  1. arm单板上移植gdb

    虚拟机 : vmware 12 image: ubuntukylin 14.04.1 系统版本:Linux dancy 3.13.0-32-generic #57-Ubuntu SMP Tue Jul ...

  2. PAT java大数 A+B和C

    题目描述: 给定区间[-, ]内的3个整数A.B和C,请判断A+B是否大于C. 输入格式: 输入第1行给出正整数T(<=),是测试用例的个数.随后给出T组测试用例,每组占一行,顺序给出A.B和C ...

  3. C语言循环结构作业总结

    循环作业总结 1.1 基本要求 按时交 - 有分 未交 - 0分 迟交一周以上 - 倒扣本次作业分数 抄袭 - 0分 博客作业不规范,没有Markdown语法 - 扣分 泛泛而谈(最多七分) 1.2 ...

  4. window+kafka

    window环境搭建zookeeper,kafka集群 为了演示集群的效果,这里准备一台虚拟机(window 7),在虚拟机中搭建了单IP多节点的zookeeper集群(多IP节点的也是同理的),并且 ...

  5. adoop集群动态添加和删除节点

    hadoop集群动态添加和删除节点说明 上篇博客我已经安装了Hadoop集群(hadoop集群的安装步骤和配置),现在写这个博客我将在之前的基础上进行节点的添加的删除. 首先将启动四台机器(一主三从) ...

  6. 感觉自己应该重新读一次Javascript

    我自己也有一本Javascript书籍,是自己上大学的时候学校给提供的,现在,我依旧带着这本书.我决定要把这本书在重新温习一下.然后,开启下面的Javascript之旅.这是我看到博客园一位园友写的, ...

  7. BZOJ4475 JSOI2015子集选取(动态规划)

    数据范围过大说明这个题和组合一点关系也没有,答案基本上肯定是ab的形式了.暴力打表感觉不太好写,找到当年的题面发现还有个样例是6 40 401898087,于是暴力找ab=401898087的数,发现 ...

  8. 【题解】SDOI2018战略游戏

    被CNST的大小卡了好久.一定要开到18呀…… 首先,遇到这种带各种各样环的图先考虑是不是可以建立圆方树,在圆方树上求出答案.然后转化为圆方树之后,我们就将图转化到了树上.答案非常的明显:只要一个圆点 ...

  9. 导致SQL执行慢的原因

    索引对大数据的查询速度的提升是非常大的,Explain可以帮你分析SQL语句是否用到相关索引. 索引类似大学图书馆建书目索引,可以提高数据检索的效率,降低数据库的IO成本.MySQL在300万条记录左 ...

  10. [Leetcode] Linked list cycle 判断链表是否有环

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...