今天是第 19 天,要来写的题目是 Palindrome Number
那麽话不多说,我们就开始吧 ─=≡Σ(((っ›´ω`‹ )っ!
# Example1
Input: x = 121
Output: true
# Example2
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
# Example3
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Palindromic number 又称做回文数,先大概来摘要一下这题的重点:
-100
相反後并没有 100-
的值一开始不知道该怎麽下手,就先从最简单的第二项不能是负数的下手吧!
func isPalindrome(x int) bool {
if x < 0 {
return false
}
return true
}
接下来就要思考,怎麽样得到相反的值勒~
应该可以使用跑回圈的方式,透过每次取余数的方式得到最後一个值再加总起来,这样就能够得到相反的数值了!
func isPalindrome(x int) bool {
if x < 0 {
return false
}
tmp := x
num := 0
for tmp > 0 {
remainder := tmp % 10
num = (num * 10) + remainder
tmp /= 10
}
res := x == num
return res
}
成功啦!
看起来 runtime 是有点点久还有待加强,但先有再求好XD
有任何问题欢迎与我告知 :)
本篇文章同步发布於 我的部落格
<<: [Day 19] - 初探永丰银行线上收款API - 订单查询及其他(1)
在ActionBattle_Var.js修改成 接着开一支新程序叫ActionBattle_Spri...
Controller test 主要测在 controller 的 action,基本的 CRUD ...
昨天也都了解到Unity脚本侦测按键的程序码是该怎麽做打的 那仔细看Unity官方提供的范例好像有两...
在Kubernetes上跑Drone CI/CD 为何我要介绍大家怎麽在K8s上跑Drone呢?因为...
还记得在中坜上课时,吴老师常说:Java因介面而伟大。 初学物件导向程序概念,还真的不太能体悟为什麽...