LeetCode(7)Reverse Integer

题目如下:

C++代码:

class Solution {
public:
    int reverse(int x) {
        if( overflow(x) == true)
        {
            return 0;
        }  

        int result = 0;  

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

        return result;
    }
private:
    bool overflow(int x)//此题要保证返回的整数位整型,确定的是x是一个整型
    {                   //故要判断x是否小于~(2^31-1),小于则没有越界
        if (x / 1000000000 == 0) // x的绝对值小于1000000000, 不越界
        {
            return false;
        } else if (x == INT_MIN) // INT_MIN反转后越界,也没法按下述方法取绝对值(需要特判),直接返回true
        {
            return true;
        }
        x = abs(x);
        // x = d463847412 ->  2147483647. (参数x,本身没有越界,所以d肯定是1或2)
        // or -d463847412 -> -2147483648.
        for (int cmp = 463847412; cmp != 0; cmp/=10, x/=10)
        {
            if ( x%10 > cmp%10 )
            {
                return true;
            } else if (x%10 < cmp%10)
            {
                return false;
            }
        }  

        return false;
    }
};
时间: 2024-08-09 23:56:21

LeetCode(7)Reverse Integer的相关文章

LeetCode之Easy篇 ——(7)Reverse Integer

7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note:Assume we are dealing with an environment which could only hold

LeetCode(25)Reverse Nodes in k-Group

题目 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 in the end should remain as it is. You may not alter the values in the nodes, only

Leetcode(1)两数之和

Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 第一种方法:暴力 执行用时:5352 ms: 内存消耗:12.9MB 效果:非常差 class Solution(object): def twoSum(self, nums, target): """ :type nums:

java基础巩固系列(二):Integer与int之间的区别

在JDK1.5之后引入了自动装箱(autoboxing)与自动拆箱(unboxing),这让很多对java的初学者感到很疑惑,我刚才也是其中一员. 首先,有一些基本的概念需要了解: 1.Ingeter是int的包装类,int的初值为0,Ingeter的初值为null. 2.Integer是一个类,用Integer声明一个变量是一个对象类型(或者说引用类型):int是基本类型,用int声明的变量是非对象类型,即不能在其上调用方法. 3."=="作用于对象上的时候,其比较的是对象的引用本身

Leetcode(4)寻找两个有序数组的中位数

Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1* 和 nums2 不会同时为空. 第一种方法:list拼接排列取中位数 执行用时:116 ms : 内存消耗:11.8MB 效果:还行 class Solution(object): def findMedianSortedArrays(self,

Leetcode(5)最长回文子串

Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 一开始我的思路如下:回文子串的特点是首尾字母相同,所以我对每一个字母都找到位于它后面的相同字母,利用切片判断这一段是否为回文子串(str[i:j]==str[i:j][::-1]).时间复杂度很高,主要是因为str.find操作非常耗时. class Solution(object): def lo

Leetcode(2)两数相加

Leetcode(2)两数相加 [题目表述]: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和.您可以假设除了数字 0 之外,这两个数都不会以 0 开头. 第一种方法:大众解法 执行用时:80 ms: 内存消耗:12.2MB # Definition for singly-linked list. # class ListNode(object

Leetcode(3)无重复字符的最长子串

Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果:太差 class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ Maxsize=0 res='' if len(s)

Leetcode(9)回文数

Leetcode(9)回文数 [题目表述]: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 第一次:直接全部转 执行用时:148 ms: 内存消耗:13.4MB 效果:还行 class Solution: def isPalindrome(self, x: int) -> bool: s=str(x) if s==s[::-1]: return True else: return False 第二种方法:反转一半数字 执行用时:156 ms: 内存消耗