[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 ...
随机推荐
- 整合hibernate的lucene大数据模糊查询
大数据模糊查询lucene 对工作单使用 like模糊查询时,实际上 数据库内部索引无法使用 ,需要逐条比较查询内容,效率比较低在数据量很多情况下, 提供模糊查询性能,我们可以使用lucene全文 ...
- C#和sqlserver中生成新的32位GUID
C#中用Guid.NewGuid().ToString() Sql中用NEWID() 以上方法生成的是36位的GUID,如果需要转换成32位,则需要替换掉其中的'-'字符. Sql中的方法:repla ...
- weblogic管理服务器密码相关
安全控制weblogic,我们可以为weblogic Administrator服务器设置密码 1.administrator服务器或受管服务器启动时,需要认证,方法有三种: (1)command启动 ...
- 阿里云高速maven库
<repository> <id>alimaven</id> <name>aliyun maven</name> <url>ht ...
- mysql 位运算
& 与运算 | 或运算 ^ 异或运算 或者 你也可以将 与运算理解为 + 法 例如 1|2 = 3 (1+2 = 3)1|2|4 = 7 (1+2+4 = 7) 将 异或运算理解为 - ...
- Unix系统编程()文件控制操作fcntl
fcntl系统调用对一个打开的文件描述符执行一系列的控制操作. int fcntl(int fd, int cmd, -) cmd参数所支持的操作范围很广 fcntl的第三个参数以省略号表示,意味着可 ...
- 如何找到文件的家-打开文件对话框openFileDialog
private void button1_Click(object sender, EventArgs e) { openFileDialog1.Filter = "*.txt|*.txt& ...
- Requests blocked by CORS policy in spring boot and angular
在本地启动Spring Boot后端和Angular前端调试时遇到跨域访问的问题导致前端请求失败. 错误描述 Access to XMLHttpRequest at 'http://localhost ...
- 打印-print.js
//打印开始// strPrintName 打印任务名// printDatagrid 要打印的datagridfunction CreateFormPage(ctx,strPrintName, pr ...
- php -- 魔术方法 之 判断属性是否存在或为空:__isset()
属性重载:当访问一个不存在或者权限不够的属性的时候,能够触发一系列的魔术方法,就叫做属性重载 __isset($name):当使用 isset()函数或者empty()函数 判断属性是否存在或者是否为 ...