每日一题:Leetcode 9.回文数
2020-03-08 14:08
381 查看
题目描述
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
示例 1:
输入: 121
输出: true
示例 2:
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
示例 3:
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
题解
题目难度:简单
解题思路:本题目可以通过把输入数据转换为字符串形式解决,也可以对整型数据是否回文直接进行判断。我们可以通过判断输入数据翻转后的值是否与原值相同来判断其回文与否(整数翻转的方法可见我的博客每日一题:Leetcode 7.整数翻转)。值得注意的是,如果数据为负值或溢出,则其一定不回文。
AC代码:
package main import "fmt" const MAX_VALUE = 2147483647 func main(){ var x int fmt.Scanf("%d", &x) fmt.Print( isPalindrome( x ) ) } //本函数用于判断参数x是否为回文数 func isPalindrome(x int) bool { if x<0 || x>MAX_VALUE { return false //如果x为负数或正值溢出,则其一定为非回文数 } x0 := x //x0用于储存x的原值 var res = 0 for ; x>0 ; x/=10 { res = res*10 + x%10 } if x0==res { return true //如果输入数据x翻转后的值res与原值x0相等,则其为回文数 } return false }
原题目链接:https://leetcode-cn.com/problems/palindrome-number
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 【leetcode每日一题】【2019-05-06】9. 回文数
- leetcode每日一练:最长回文子串(java)
- 【leetcode每日一题】【2019-04-16】5.最长回文子串
- LeetCode每日一题——T5. 最长回文子串(中):中心扩散法、未完待续
- leetcode 每日一题 203. Remove Linked List Elements
- [LeetCode] Longest Palindromic Substring 最长回文子串
- leetcode 每日一题 242. Valid Anagram
- leetcode 每日一题 206. Reverse Linked List
- leetcode 最长回文子串
- leetcode:299 Bulls and Cows-每日编程第四十一题
- [LeetCode系列] 最长回文子串问题
- LeetCode之回文数判断
- 力扣(LeetCode 9)回文数 python
- Leetcode234 单链表+回文判断
- LeetCode每日一题(2018-01-25)
- LeetCode 234. Palindrome Linked List (回文链表)
- leetcode - 9 - 回文数
- LeetCode---5. Longest Palindromic Substring(最长回文子串)
- leetcode 479. Largest Palindrome Product最大的回文乘积
- (LeetCode每日一刷04)罗马数字转整数