摘要:難度本題要求判定一個(gè)整數(shù)是否為回文數(shù)字比如都是回文數(shù)字但是不是回文數(shù)字所有負(fù)數(shù)都不是回文數(shù)字本題還有一個(gè)關(guān)鍵要求不能使用額外空間我理解這里的額外空間是指堆空間在程序中不能去額外的什么變量更不用說提升空間復(fù)雜度直接上的解法解法
Determine whether an integer is a palindrome. Do this without extra space.
Some hints: Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the
restriction of using extra space.You could also try reversing an integer. However, if you have solved
the problem "Reverse Integer", you know that the reversed integer
might overflow. How would you handle such case?There is a more generic way of solving this problem.
難度: Easy
本題要求判定一個(gè)整數(shù)是否為回文數(shù)字, 比如 1, 121, 都是回文數(shù)字, 但是-1, 12, 不是回文數(shù)字.
所有負(fù)數(shù)都不是回文數(shù)字.
本題還有一個(gè)關(guān)鍵要求, 不能使用額外空間. 我理解這里的額外空間是指堆空間. 在程序中不能去額外的new 什么變量, 更不用說提升空間復(fù)雜度.
直接上AC的解法:
public class Solution { public boolean isPalindrome(int x) { if (x < 0) { return false; } int digits = 0; int tmp = x; while (tmp != 0) { tmp = tmp / 10; digits++; } for (int i = 1; i <= digits / 2; i++) { if (getDigit(x, i) != getDigit(x, digits + 1 - i)) { return false; } } return true; } private int getDigit(int target, int pos) { for (int i = 0; i < pos - 1; i++) { target = target / 10; } return target % 10; } public static void main(String[] args) { Solution s = new Solution(); System.out.println(s.isPalindrome(1)); System.out.println(s.isPalindrome(12)); System.out.println(s.isPalindrome(11)); System.out.println(s.isPalindrome(121)); System.out.println(s.isPalindrome(1212)); System.out.println(s.isPalindrome(-1)); System.out.println(s.isPalindrome(-11)); System.out.println(s.isPalindrome(-121)); System.out.println(s.isPalindrome(-2147447412)); } }
解法很直接, 從兩頭開始往中間判定數(shù)字是否相同, 不同直接返回false.
其中getDigit方法是獲取這個(gè)數(shù)的第n位數(shù)字, 為了滿足不使用額外空間的要求, 這么解實(shí)際上提升了時(shí)間復(fù)雜度.
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/66510.html
摘要:有一點(diǎn)需要注意的是,負(fù)數(shù)不算作回文數(shù)。而第題當(dāng)時(shí)的方法是,對(duì)整數(shù)取除的余數(shù),即是當(dāng)前整數(shù)的最后一位。那么它翻轉(zhuǎn)后一半的數(shù)字之后,應(yīng)該和前半段的數(shù)字相等,我們將采用這種思路進(jìn)行解題。 題目詳情 Determine whether an integer is a palindrome. Do this without extra space.題目要求我們?cè)诓徽加妙~外空間的前提下,判斷一個(gè)整...
摘要:反轉(zhuǎn)比較法復(fù)雜度時(shí)間空間思路回文數(shù)有一個(gè)特性,就是它反轉(zhuǎn)后值是一樣的。代碼逐位比較法復(fù)雜度時(shí)間空間思路反轉(zhuǎn)比較有可能會(huì)溢出,但我們遍歷每一位的時(shí)候其實(shí)并不用保存上一位的信息,只要和當(dāng)前對(duì)應(yīng)位相等就行了。首先,負(fù)數(shù)是否算回文。 Palindrome Number Determine whether an integer is a palindrome. Do this witho...
摘要:題目分析這是上的第題,難度為,判斷整型數(shù)字是否為回文串,需要注意兩點(diǎn)負(fù)數(shù)都不是回文小于的非負(fù)整數(shù)都是回文這一題與第題類似,也可以有兩種思路數(shù)組法和模十法。所以代碼可以如下改進(jìn)能被整除的非整數(shù)和負(fù)數(shù),返回 題目 Determine whether an integer is a palindrome. Do this without extra space. 分析 這是leetcode上...
摘要:最后,我們判斷一開始的兩種情況,并返回或者即可。本許可協(xié)議授權(quán)之外的使用權(quán)限可以從處獲得。 Create by jsliang on 2019-05-22 19:30:42 Recently revised in 2019-05-23 11:42:52 一 目錄 不折騰的前端,和咸魚有什么區(qū)別 目錄 一 目錄 二 前言 三 解題 ?3.1 解題 - 數(shù)組操作 ...
摘要:首尾比較法復(fù)雜度時(shí)間空間,為所求的長度思路先求記為的長度根據(jù)長度制造掩碼循環(huán)當(dāng)當(dāng)最高位等于最低位還有數(shù)字等待判斷最高位通過掩碼和整除取得,最低位通過取余取得判斷過后更新掩碼,刪掉最高位,刪掉最低位注意求長度的如何取得一個(gè)的最高位假設(shè)答設(shè)置一 Palindrome Number Determine whether an integer is a palindrome. Do this w...
閱讀 2061·2021-09-26 10:19
閱讀 3318·2021-09-24 10:25
閱讀 1783·2019-12-27 11:39
閱讀 2034·2019-08-30 15:43
閱讀 764·2019-08-29 16:08
閱讀 3584·2019-08-29 16:07
閱讀 975·2019-08-26 11:30
閱讀 1333·2019-08-26 10:41