LeetCode OJ: 9. Palindrome Number (C++)
題目:Determine whether an integer is a palindrome. Do this without extra space.
解法1、需要額外空間:
儲存各位數字,逆轉,換回逆轉值,與原值比較
class Solution { public: int reverse(int x) { long long sum=0; long long result=0; bool negative=false; vector<int> temp; if(x<0){ negative=true; sum=-x; }else{ sum=x; } while(sum>=10){ temp.push_back(sum%10); sum=sum/10; } temp.push_back(sum); bool isnotZore=false; for(int i=0;i<temp.size();i++){ if(temp[i]!=0){ isnotZore=true; } if(isnotZore){ if(negative){ result*=10; result-=temp[i]; }else{ result*=10; result+=temp[i]; } } } if(result>~(1<<31)){ return 0; }else if(result<(1<<31)){ return 0; }else { return result; } } bool isPalindrome(int x) { if(x<0) return false; return x==reverse(x); } };
解法2、不需額外空間
每次,取出數的最高位和最低位比較,這裡設定一個base為10^n,用來取出數的最高位,每次迴圈除以100,因為每次數會消去2位。
class Solution { public: bool isPalindrome(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function if (x < 0) return false; if (x == 0) return true; int base = 1; while(x / base >= 10) base *= 10; while(x) { int leftDigit = x / base; int rightDigit = x % 10; if (leftDigit != rightDigit) return false; x -= base * leftDigit; base /= 100; x /= 10; } return true; } };
相關推薦
LeetCode OJ: 9. Palindrome Number (C++)
題目:Determine whether an integer is a palindrome. Do this without extra space. 解法1、需要額外空間: 儲存各位數字,
LeetCode OJ 之 Palindrome Number(迴文數字)
題目:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie,
LeetCode 9. 迴文數 Palindrome Number(C語言)
題目描述: 判斷一個整數是否是迴文數。迴文數是指正序(從左向右)和倒序(從右向左)讀都是一樣的整數。 示例 1: 輸入: 121 輸出: true 示例 2: 輸入: -121 輸出: false 解釋: 從左向右讀, 為 -121 。 從右向左
LeetCode 9. Palindrome Number(回文數)
num bre spa new NPU amp public center integer Determine whether an integer is a palindrome. An integer is a palindrome when it reads the
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 O
9. Palindrome Number(迴文數)C++
將int轉換為string,注意for迴圈判斷條件的簡化 class Solution { public: bool isPalindrome(int x) { if(x < 0) return false; string s =
LEETCODE 9 Palindrome Number (JAVA題解)
https://leetcode.com/problems/palindrome-number/ 原題如上。 題意解析: 判斷一個整數是否是迴文整數。像123321,,121,這樣的,就是迴文整數
9. Palindrome Number(python+cpp)
題目: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Inp
【LeetCode】754. Reach a Number(C++)
題目: You are standing at position 0 on an infinite number line. There is a goal at position target. On each move, you can either go
[LeetCode] Algorithms-9. Palindrome Number
mcs npe mar algo etc lan targe href leet U骨沿17貿V日禱吞3http://weibo.com/p/1005055847889847 17北ZPF皇3懶Rhttp://huiyi.docin.com/sina_6269974098
leetcode題解 9. Palindrome Number
ber HR 哈哈 tin using scrip 情況 == while 9. Palindrome Number 題目: Determine whether an integer is a palindrome. Do this without extra space.
【leetcode】 9. palindrome number
ble number cli ack ont art ews styles doc @requires_authorization @author johnsondu
【LeetCode】9. Palindrome Number - Java實現
文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Determine whether an integer is a palindrome. An integer is a palindrome
【LeetCode】56. Merge Intervals(C++)
地址:https://leetcode.com/problems/jump-game/ 題目: https://leetcode.com/problems/merge-intervals/ Example 1: Example 2: 理解: 在有序的情況下,直接從頭到尾和
【LeetCode】48. Rotate Image(C++)
地址:https://leetcode.com/problems/rotate-image/ 題目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (c
【LeetCode】47. Permutations II(C++)
地址:https://leetcode.com/problems/permutations-ii/ 題目: Given a collection of numbers that might contain duplicates, return all possible unique
【LeetCode】43. Multiply Strings(C++)
地址:https://leetcode.com/problems/multiply-strings/ 題目: Given two non-negative integers num1 and num2 represented as strings, return the produc
【LeetCode】39. Combination Sum(C++)
地址:https://leetcode.com/problems/combination-sum/ 題目: Given a set of candidate numbers (candidates) (without duplicates) and a target number (
【LeetCode】36. Valid Sudoku(C++)
地址:https://leetcode.com/problems/valid-sudoku/ 題目: Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated accord
【LeetCode】31. Next Permutation(C++)
地址:https://leetcode.com/problems/next-permutation/ 題目: Implement next permutation, which rearranges numbers into the lexicographically next gr