LeetCode 9 的题目是 “Palindrome Number”(回文数)。题目要求如下: 题目描述: 给定一个整数 x,判断它是否是一个回文数。回文数是指正着读和倒着读都一样的整数。 示例: 输入: 121 输出: true 输入: -121 输出: false 解释: 倒着读是 121-,所以不是回文数。可见,所有负数都不是回文数字。 输入: 10...
LeetCode 9. Palindrome Number(c语言版) 题目: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input:121Output:true Example 2: Input:-121Output:falseExplanation:From left to right, it reads -121. From right to...
1classSolution {2public:3boolisPalindrome(intx) {4if(x <0)returnfalse;5if(x <10)returntrue;6intm =1;//long long m = 17while(x / m >=10) {//x / m > 08m *=10;9}10//m /= 10;11intl, r;12while(m >1) {13l = x /m;14x = x %m;15m /=100;16r = x %10;17...
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 1. Output: true 1. Example 2: Input: -121 1. Output: false 1. Explanation: From left to right, it reads -121. From right to left, it becomes...
public boolean isPalindrome(int x) { if(x < 0 || (x % 10 == 0 && x != 0)) return false; int revertedNumber = 0; while(x > revertedNumber) { revertedNumber = revertedNumber * 10 + x % 10; x /= 10; } return x == revertedNumber || x == revertedNumber/10; } 时间复...
内存消耗:5.3 MB, 在所有 Go 提交中击败了19.58%的用户 leetcode执行: Runtime: 28 ms, faster than 21.90% of Go online submissions for Palindrome Number. Memory Usage: 5.6 MB, less than 14.02% of Go online submissions for Palindrome Number. 1. 2. 3. 4. 5. 6. 7....
1241 Number of Comments per Post * $ 68.00% Easy 1240 Tiling a Rectangle with the Fewest Squares 52.40% Hard 1239 Maximum Length of a Concatenated String with Unique Characters 50.70% Medium 1238 Circular Permutation in Binary Representation 67.90% Medium 1237 Find Positive Integer Solution for a...
0476 Number Complement Go 67.1% Easy 0477 Total Hamming Distance Go 52.2% Medium 0478 Generate Random Point in a Circle Go 39.6% Medium 0479 Largest Palindrome Product 31.6% Hard 0480 Sliding Window Median Go 41.4% Hard 0481 Magical String 50.4% Medium 0482 License Key Formatting 43.2...
funcisPalindrome(_ x:Int)->Bool{var str=String()letchas=String(x).reversed()forcinchas{str.append(c)}ifString(x)==str{returntrue}returnfalse} 【思路2】 1、从x的各位到最高位 依次遍历得到一个新数值,判断两个数值是否相等 2、时间复杂度O(log10ºn),(以十为底n的对数)因为每次都会除以10...
classSolution{public:boolisPalindrome(intx){if(0>x||(x%10==0&&x!=0)){returnfalse;}intreversed=0;while(x>reversed){reversed=reversed*10+x%10;x=x/10;}returnx==reversed||x==reversed/10;}}; NOTE(注) 思路1:转字符串 将整形转换成字符串,从两侧向内依次比较即可。既然题目提示了不转换字...