[LeetCode] 9.Palindrome Number - Swift
Determine whether an integer is a palindrome. Do this without extra space.
题目意思:判断一个整数是否是回文数
例如:123211,1221即为回文数,110则不是回文数
===================================================================
思路一:
现将这个整数反转,然后判断返转后的数是否与原来的是相等,相等则为回文数,反之则不是。
利用了上一题[LeetCode] 7.Reverse Integer - Swift的算法
实现代码:
class Solution {
func isPalindrome(_ x: Int) -> Bool {
if x <= 0 {
return false
}
var tmp: Int = x
var str: String = ""
var reverseX: Int = 0
while tmp/10 != 0 {
str = str.appending("\(tmp%10)")
tmp = tmp/10
}
str = str.appending("\(tmp)")
reverseX = Int(str)!
if reverseX == x {
return true
}
return false
}
}
===================================================================
思路二:(思路一的效率略低,参考了别人的思路)
整型分为两种情况:
第一种情况:奇数位,如121,走完while循环后leaveNum=1,newNum=12,根据leaveNum==newNum/10,则返回true,判断得出121是回文数
第二种情况:偶数位,如1221,走完while循环后leaveNum=12,newNum=12,根据leaveNum==newNum,则返回true,判断得出1221是回文数
这种方法循环执行的次数是思路一方法的一半,效率自然高!
class Solution {
func isPalindrome(_ x: Int) -> Bool {
if x < 0 || (x != 0 && x % 10 == 0) {
return false
}
var leaveNum = x
var newNum = 0
while leaveNum > newNum {
newNum = newNum * 10 + leaveNum % 10
leaveNum = leaveNum / 10
}
return (newNum == leaveNum || leaveNum == newNum / 10)
}
}
[LeetCode] 9.Palindrome Number - Swift的更多相关文章
- Leetcode练习题 Palindrome Number
9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...
- Leetcode 9. Palindrome Number(水)
9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- LeetCode 9. Palindrome Number (回文数字)
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- [LeetCode][Python]Palindrome Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- [LeetCode] 9. Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 【leetcode】Palindrome Number
题目简述: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could n ...
随机推荐
- shell取余数
shell取余数 技术分享 » linux | 阅读(9993) | 评论(0) Sep 3 2010 备忘脚本date取得分钟数$(()) 运算 #execute every 5 minutesa= ...
- Xcode常见问题
今天真机测试的时候,突然出现了这个错误: not have an architecture that “Administrator”的 iPhone (3) can execute. 原因是我刚刚修 ...
- Vs code 通用插件
Vs code 通用插件 转自:https://segmentfault.com/a/1190000006697219 HTML Snippets 超级实用且初级的 H5代码片段以及提示 HTML C ...
- js控制伪元素样式
//获取伪元素// CSS代码 #myId:before { content: "hello world!"; display: block; width: 100px; heig ...
- FreeRTOS 调度锁,任务锁和中断锁
以下转载自安富莱电子: http://forum.armfly.com/forum.php 调度锁调度锁就是 RTOS 提供的调度器开关函数,如果某个任务调用了调度锁开关函数,处于调度锁开和调度锁关之 ...
- Roslyn介绍
介绍 一般来说,编译器是一个黑箱,源代码从一端进入,然后箱子中发生一些奇妙的变化,最后从另一端出来目标文件或程序集.编译器施展它们的魔法,它们必须对所处理的代码进行深入的理解,不过相关知识不是每个人都 ...
- with as 和update ,Delete,insert
这个SQL写了很久的时间,感觉pgSQL的很是麻烦. with as 先命名一个表出来,就可以当成临时表用. WITH tmp AS ( SELECT MAX(mgi.inner_cd) AS inn ...
- [misc]如何在嵌入式平台使用printf功能
转自:http://www.cnblogs.com/liu_xf/archive/2011/04/14/2015726.html 摘要: 当我们在调试代码时,通常需要将程序中的某个变量打印至PC机上, ...
- 微信小程序 - mixins
mixins 概念 可百度 参考 http://ask.seowhy.com/article/21007 大意和Python中的多重继承, java中的接口类似(java接口只是定义,实现需要子类自 ...
- SSIS 自测题-数据流控件类
说明:以下是自己的理解答案,不是标准的答案,如有不妥烦请指出. 有些题目暂时没有答案,有知道的请留言,互相学习,一起进步. 133.请描述一下 Conditional Split 的使 ...