【LeetCode-面试算法经典-Java实现】【007-Reverse Integer(翻转整数)】

【007-Reverse Integer(翻转整数)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  Reverse digits of an integer.

  Example1: x = 123, return 321

  Example2: x = -123, return -321

题目大意

  输入一个整数对其进行翻转

解题思路

  通过求余数求商法进行操作。

代码实现

public class Solution {
    public int reverse(int x) {
        long tmp = x;
        // 防止结果溢出
        long result = 0;

        while (tmp != 0) {
            result = result * 10 + tmp % 10;
            tmp = tmp / 10;
        }

        // 溢出判断
        if (result < Integer.MIN_VALUE || result > Integer.MAX_VALUE) {
            result = 0;
        }

        return (int) result;
    }
}

评测结果

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/46938355

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-22 03:18:24

【LeetCode-面试算法经典-Java实现】【007-Reverse Integer(翻转整数)】的相关文章

[LintCode] Reverse Integer 翻转整数

Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). Have you met this question in a real interview? Example Given x = 123, return 321 Given x = -123, return -321 LeetCode上的原题,请参见我之前的博客Reverse Integer.

[LeetCode] Reverse Integer 翻转整数

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought throu

【LeetCode-面试算法经典-Java实现】【151-Evaluate Reverse Polish Notation(计算逆波兰式)】

[151-Evaluate Reverse Polish Notation(计算逆波兰式)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some

LeetCode 007 Reverse Integer

[题目] Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 [题意] 反转int型整数,输出的也是int型的整数 [思路] 如要考虑两种特殊情况: 1. 类似100这样的整数翻转之后为1 2. 翻转之后的值溢出该如何处理, 本题的测试用例中似乎没有给出溢出的情况 在实际面试时需要跟面试官明确这种情况的处理方法. 基于这点事实,本题规定如果超出正边界返回INT_MA

【LeetCode-面试算法经典-Java实现】【025-Reverse Nodes in k-Group(单链表中k个结点一组进行反转)】

[025-Reverse Nodes in k-Group(单链表中k个结点一组进行反转)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes i

【LeetCode-面试算法经典-Java实现】【009-Palindrome Number(回文数)】

[009-Palindrome Number(回文数)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Determine whether an integer is a palindrome. Do this without extra space. 题目大意 判断一个数字是否是回访字数,不要使用额外的空间. 解题思路 为了不使用额外的空间,参考了其它的解决,那些解法看起来在isPalindrome方法中没有使用额外参数,但是却使用了方法调用,这个比一个整数消耗的

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", di

【LeetCode-面试算法经典-Java实现】【152-Reverse Words in a String(反转字符串中的单词)】

[152-Reverse Words in a String(反转字符串中的单词)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". 题目大意 给定一个字符串,将其反转,其的字词不转 解题思路

【LeetCode-面试算法经典-Java实现】【092-Reverse Linked List II(反转单链表II)】

[092-Reverse Linked List II(反转单链表II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->