Leetcode 9 Palindrome Number
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121 Output: true
Example 2:
Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
題意即為判斷是否是迴文數,可以使用棧和佇列進行實現或者是使用遞迴來實現
《one》
import java.util.*;
class Solution {
public boolean isPalindrome(int x) {
if(x < 0){
return false;
}
int y = x;
Queue<Integer> queue = new LinkedList<>();
Stack<Integer> stack = new Stack<>();
while(y != 0){
queue.offer(y % 10);
stack.push(y % 10);
y /= 10;
}
while(!stack.isEmpty()&&!queue.isEmpty()){
if(stack.pop() != queue.poll()){
return false;
}
}
return true;
}
}
《Two》
public boolean isPalindrome(int x) {
// 如果是負數,或者10的整數倍,返回false
if (x < 0 || x != 0 && x % 10 == 0)
return false;
int reverse = 0;
while (x > reverse) {
reverse = reverse * 10 + x % 10;
x = x / 10;
}
return (reverse == x || reverse / 10 == x);
}
相關文章
- [LeetCode] 9. Palindrome NumberLeetCode
- LeetCode Palindrome Number(009)解法總結LeetCode
- LeetCode - 解題筆記 - 8 - Palindrome NumberLeetCode筆記
- leetcode學習筆記09 palindrome-numberLeetCode筆記
- leetcode第九題Palindrome Number 驗證迴文數字LeetCode
- 牛課題霸--palindrome-number
- [LeetCode/LintCode] Largest Palindrome ProductLeetCode
- [LeetCode] 336. Palindrome PairsLeetCodeAI
- Leetcode 234. Palindrome Linked ListLeetCode
- Leetcode Number of islandsLeetCode
- [LeetCode] Third Maximum NumberLeetCode
- [LeetCode] Find the Duplicate NumberLeetCode
- Leetcode 447 Number of BoomerangsLeetCodeOOM
- Leetcode 933 Number of Recent CallsLeetCode
- LeetCode之Fibonacci Number(Kotlin)LeetCodeKotlin
- Leetcode 611 javascript Valid Triangle NumberLeetCodeJavaScript
- LeetCode之Number of Recent Calls(Kotlin)LeetCodeKotlin
- [LeetCode] 248. Strobogrammatic Number IIILeetCode
- [LeetCode] 191. Number of 1 BitsLeetCode
- [LeetCode] 305. Number of Islands IILeetCode
- Leetcode 17 Letter Combinations of a Phone NumberLeetCode
- Leetcode 137. Single Number IILeetCode
- 【Leetcode】1395. Count Number of TeamsLeetCode
- Leetcode – 017. Letter Combinations of a Phone NumberLeetCode
- [LeetCode] 3238. Find the Number of Winning PlayersLeetCode
- [LeetCode] 2684. Maximum Number of Moves in a GridLeetCode
- Leetcode 202 Happy Number Javascript 解決方案LeetCodeAPPJavaScript
- [LeetCode] 2406. Divide Intervals Into Minimum Number of GroupsLeetCodeIDE
- LeetCode Letter Combinations of a Phone Number(017)解法總結LeetCode
- Leetcode 之 PHP 解析 (260. Single Number III)LeetCodePHP
- LeetCode演算法題-Number of Boomerangs(Java實現)LeetCode演算法OOMJava
- LeetCode65. Valid Number — 判斷合法數字LeetCode
- [LeetCode] 1953. Maximum Number of Weeks for Which You Can WorkLeetCode
- [LeetCode] 3226. Number of Bit Changes to Make Two Integers EqualLeetCode
- LeetCode 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance??LeetCode
- LeetCode 452. Minimum Number of Arrows to Burst Balloons Sort/MediumLeetCode
- [LeetCode] 3239. Minimum Number of Flips to Make Binary Grid Palindromic ILeetCode
- Leetcode 1365. How Many Numbers Are Smaller Than the Current Number (cpp)LeetCode