Leecode題解 :7. Reverse Integer

2021-03-02 程式設計師大白

點擊上方「程式設計師大白」,選擇「星標」公眾號

重磅乾貨,第一時間送達


題目分析

題目意思,要求反轉一個數字,如果反轉後超過32位表示,則輸出0

解題思路(1)

這道題非常簡單,可能容易錯誤的是,最後怎麼判斷是否超過了32位,我們的limit.h中的INT_MAX,INT_MIN提供了,那麼我們專心考慮如何反轉即可。  

我們可以用字符串進行保存,然後調用反轉方法,最後重新轉發為數字即可。最終判斷一下是否屬於32位範圍之內即可。

解題思路(2)

我們可以不用這麼麻煩,先轉化為字符串再做,而是直接進行反轉即可,我們可以對輸入的x,進行判斷,如果while(x!=0),一直進行s = s*10+x%10,x/=10的操作,最終跳出循環的時候,已經實現了反轉操作。  

#####舉個例子解釋一下

當輸入為123時候,  

while(123!=0)

s = 0*10+123%10 = 3

x = 123/10 = 12

while(12!=0)

s = 3*10+12%10 = 32

x = 12/10 = 1

while(1!=0)

s = 32*10+1%10 = 321

x = 1/10 = 0  

這個時候x為0,跳出循環,已經完成了轉換過程!

CPP代碼

class Solution {
public:
    int reverse(int x) {
       long long  int s = 0;              //它有可能會超過int的值
        while(x!=0)
        {
            s = s*10+x%10;                //進行反轉
            x/=10;
        }
        if(s>INT_MAX || s<INT_MIN)
            return 0;
        else
            return s;
    }
};

推薦閱讀

Leecode題解:6-convert

Leecode題解:4.Median of Two Sorted Arrays

Leecode題解:3-length Of Longest Substring

Leecode題解:1.TwoSum

Leetcode編程分享——&nbsp;盛最多水的容器

開源!TensorFlow 2.0中文開源書項目

談談我在PyTorch踩過的12坑

關於程式設計師大白

程式設計師大白是一群哈工大,東北大學,西湖大學和上海交通大學的碩士博士運營維護的號,大家樂於分享高質量文章,喜歡總結知識,歡迎關注[程式設計師大白],大家一起學習進步!

相關焦點

  • LeetCode-7. Reverse Integer | 整數反轉
    If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
  • Leecode題解 CombinationSum
    舉個例子:數字集合: [2, 3, 6, 7]目標值: 7結果: [     [7],     [2, 2, 3]]題解樣例數字集合: [2, 3, 6, 7]目標值: 7回溯算法找到的第1個組合是[2,2,2],因為target = 1小於當前處理數組[2,3,6,7]的最小值,返回上一層(剪枝)。
  • leetcode-7. Reverse Integer
    public class Reverse_Integer { public static int reverse(int x) {  int rev=0;  while(x!如果 rev == intMax / 10 = 2147483647 / 10 = 214748364 ,此時 rev * 10 就是 2147483640 如果 pop 大於 7 ,那麼就一定溢出了。但是!
  • Leecode題解 :4.Median of Two Sorted Arrays
    題解算法及複雜度(35 ms)  這個問題歸結為為查找兩個數組合併後的第 k 小元素.比較這兩個數, 有三種可能性 >, =, <,如果是 = 的情況,那麼nums1[k / 2 - 1]就是兩個數組合併後的第k小的數(可以自己簡單舉個例子試一下,比如[1, 3, 5, 6]和[0, 3, 7]), 如果是 < 的情況,可以得出結論,nums1 中 0 到 k / 2 - 1 的元素均小於兩數組的第 k 小元素.
  • Leetcode題解 WordBreak
    上述將返回true,因為"leetcode"可以分割成"leet code".題解算法及複雜度(6 ms)  本問題是確定一個字符串是否能分割成多個單詞,這些單詞都在字典中出現."d"不存在於字典中, "od"不存在於字典中,"cod"不存在於字典中,"tcod"不存在於字典中,"etcod"不存在於字典中,"eetcod"不存在於字典中,"leetcod"不存在於字典中,dp[7] = false// i = 8,"e"不存在於字典中,"de"不存在於字典中, "ode"不存在於字典中,"code"存在於字典中,且dp[i - len("code")] =
  • [每日一題]25. Reverse Nodes in k-Group
    Reverse Nodes in k-GroupGiven a linked list, reverse the nodes of a linked list k at a time and return its modified list.
  • LeetCode刷題實戰7:整數反轉
    所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 整數反轉  ,這道題很有意思,我們先來看題面:題意Given a 32-bit signed integer, reverse digits of an integer.https://leetcode.com/problems/reverse-integer/
  • Leetcode題解 Gray Code
    格雷碼:在一組數的編碼中,若任意兩個相鄰的代碼只有一位二進位數不同,則稱這種編碼為格雷碼(Gray Code)解題思路:   簡單的模擬生成題,由于格雷碼要求相鄰的數字有且僅有一位不同,先考慮如果當前已經有不大於N-1位的合法格雷碼序列
  • java面試技巧:Integer和int的那些事
    *@serial*/private final int value;/**Constructs a newly allocated {@code Integer} object thatrepresents the specified {@code int} value.** @param**/valuethe value to be represented by the{@code Integer
  • Reverse Vowels of a String && 1. Two Sum
    反轉字符串中的元音字母Write a function that takes a string as input and reverse only the vowels of a string.編寫一個函數,以字符串作為輸入,反轉該字符串中的元音字母。
  • 你應該知道25道JavaScript面試題
    ,看懂了還有興趣的可以看下這篇 玉伯的一道課後題題解(關於 IEEE 754 雙精度浮點型精度損失)九Discuss possible ways to write a function isInteger(x) that determines if x is an integer.
  • 一題一解(36)- How many pairs of 4-digit palindromes?
    一題:A palindrome is a positive integer whose digits are the same when
  • 吊打面試官系列:說說Integer緩存範圍
    * 程序第一次使用Integer的時候     * The size of the cache may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
  • LeetCode 攻略 - 2019 年 7 月下半月匯總(100 題攻略)
    | 目錄 || --- | | 一 目錄 | | 二 前言 || 三 匯總 ||  3.1 LeetCode 已攻略 ||  3.2 Function & Object || 四 總結 |001 - 兩數之和(two-sum) ✔007 - 整數反轉(reverse-integer) ✔009 - 迴文數(palindrome-number