摘要:原題目為難度此題讓我們輸出給定一個(gè)整數(shù)的倒序數(shù)比如倒序?yàn)榈剐驗(yàn)榈侨绻剐虻倪^(guò)程中發(fā)生整型溢出我們就輸出倒序不復(fù)雜關(guān)鍵在于如何判定將要溢出最終的程序如下其中是獲取的個(gè)位數(shù)字判定下一步是否將要溢出使用
原題目為:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321Have 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.
難度: Easy
此題讓我們輸出給定一個(gè)整數(shù)的倒序數(shù), 比如123倒序?yàn)?21, -123倒序?yàn)?321. 但是如果倒序的過(guò)程中發(fā)生整型溢出, 我們就輸出0.
倒序不復(fù)雜, 關(guān)鍵在于如何判定將要溢出.
最終AC的程序如下:
public class Solution { public int reverse(int x) { int x1 = Math.abs(x); int rev = 0; while (x1 > 0) { if (rev > (Integer.MAX_VALUE - (x1 - (x1 / 10) * 10)) / 10) { return 0; } rev = rev * 10 + (x1 - (x1 / 10) * 10); x1 = x1 / 10; } if (x > 0) { return rev; } else { return -rev; } } }
其中 x1 - (x1 / 10) * 10 是獲取x1的個(gè)位數(shù)字, 判定下一步是否將要溢出, 使用 rev > (Integer.MAX_VALUE - (x1 - (x1 / 10) * 10)) / 10 .
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/66514.html
摘要:字符串法復(fù)雜度時(shí)間空間思路先將數(shù)字轉(zhuǎn)化為字符串,然后將字符串倒序輸出,并轉(zhuǎn)回?cái)?shù)字。模十法復(fù)雜度時(shí)間空間思路通過(guò)對(duì)數(shù)字模十取余得到它的最低位。除了檢查溢出返回特定值以外,有沒(méi)有別的方法處理溢出可以使用代碼塊排除異常。 Reverse Integer Reverse digits of an integer.Example1: x = 123, return 321Example2: x ...
摘要:題目詳情題目要求我們給出一個(gè)數(shù)的翻轉(zhuǎn)數(shù)想法這道題主要的坑就是在于一個(gè)數(shù)值的輸入,在進(jìn)行翻轉(zhuǎn)操作之后,不一定還符合的范圍,可能會(huì)造成異常。我們可以通過(guò)每次獲得整數(shù)除的余數(shù),來(lái)確定當(dāng)前整數(shù)的最后一位。 題目詳情 Given a 32-bit signed integer, reverse digits of an integer.題目要求我們給出一個(gè)數(shù)的翻轉(zhuǎn)數(shù) Example 1:Inpu...
摘要:第一時(shí)間想到這是經(jīng)典的取模取余運(yùn)算,但是寫(xiě)的過(guò)程中遇到了很多問(wèn)題這么簡(jiǎn)單一題基礎(chǔ)做法取一個(gè)整數(shù)的最后一位數(shù)字只要把這個(gè)整數(shù)就可以,要取除最后一位數(shù)字之外的其它數(shù)字只要是沒(méi)有長(zhǎng)度函數(shù)的,需要轉(zhuǎn)化成才能使用長(zhǎng)度函數(shù)用這個(gè)方法最大的難點(diǎn)在 Easy 007 Reverse Integer Description: Given a 32-bit signed integer, reverse ...
摘要:判斷溢出這里使用了中的類整數(shù)類,縮寫(xiě)就是的靜態(tài)變量和,就能直接得到整型變量可表示數(shù)值的上下限。當(dāng)結(jié)果不在此范圍內(nèi)時(shí),則溢出,并返回否則返回正常結(jié)果。 要點(diǎn) 這一題的要點(diǎn)有三個(gè): 接收長(zhǎng)度不同的數(shù)字并翻轉(zhuǎn) 判斷結(jié)果是否溢出 解決方法 翻轉(zhuǎn):為了能夠接收不同長(zhǎng)度的數(shù)字進(jìn)行反轉(zhuǎn)操作,我們使用循環(huán)結(jié)構(gòu)進(jìn)行操作。(注:這里創(chuàng)建的sum變量一定要用long類型而不能用int,原因是采用int...
摘要:詳細(xì)介紹將其他值轉(zhuǎn)成數(shù)字值。此方法更改數(shù)組的長(zhǎng)度。詳細(xì)介紹解題思路首先,將傳入的數(shù)字轉(zhuǎn)換成字符串,并分割成數(shù)組。本許可協(xié)議授權(quán)之外的使用權(quán)限可以從處獲得。 Create by jsliang on 2019-05-19 09:42:39 Recently revised in 2019-05-19 16:08:24 Hello 小伙伴們,如果覺(jué)得本文還不錯(cuò),記得給個(gè) star , 小伙伴們...
閱讀 2433·2023-04-26 02:54
閱讀 2348·2021-10-14 09:43
閱讀 3418·2021-09-22 15:19
閱讀 2867·2019-08-30 15:44
閱讀 2729·2019-08-30 12:54
閱讀 1011·2019-08-29 18:43
閱讀 1960·2019-08-29 17:12
閱讀 1354·2019-08-29 16:40