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的更多相关文章

  1. Leetcode练习题 Palindrome Number

    9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...

  2. Leetcode 9. Palindrome Number(水)

    9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...

  3. leetcode 9 Palindrome Number 回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  4. LeetCode 9. Palindrome Number (回文数字)

    Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...

  5. Leetcode 9. Palindrome Number(判断回文数字)

    Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...

  6. [LeetCode][Python]Palindrome Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/palindr ...

  7. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  8. [LeetCode] 9. Palindrome Number 验证回文数字

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  9. 【leetcode】Palindrome Number

    题目简述: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could n ...

随机推荐

  1. iOS 关于图片地理位置隐私信息的分析和读取

    今天突然想到微信朋友圈发照片,涉及个人隐私的地理位置是否外泄.因为iphone拍照的照片都会带有地理位置等信息,我们先来实现怎么读取里面的安全信息,然后再来分析 #import "ViewC ...

  2. Unity3D学习(九):C#和C++的相互调用

    前言 不知不觉已经一年了,这一年来一直忙于公司项目疯狂加班,很少有自己的时间写下东西.不过好在项目最近也步入正轨了,正好抽空写点东西记录下学到的一些东西. 公司项目是一个端游IP移植手游,端游是基于C ...

  3. 从乌云的错误漏洞分析看Mifare Classic安全

    前言 12年2月初国内著名安全问题反馈平台-乌云发布了有关某公司员工卡的金额效验算法破解的安全问题.从整个漏洞分析来看,漏洞的提交者把员工卡的数据分析得非常仔细,以至很多刚刚接触或者未曾接触的都纷纷赞 ...

  4. python学习笔记(8)--爬虫下载占位图片

    说明: 1. 虽然很简单,但总忘,所以还是记下来吧! 2. http://placekitten.com/300/300这个画猫的网站老是打开没有图片,改成了http://placehold.it/3 ...

  5. JSON 文件格式解析

    JSON 文件大致说明 JSON 文件你可以理解为就是一个字典文件. 格式为 { 索引:数据, 索引:{ 索引:数据, 索引:{ 索引:数据, 索引:数据 } } } 自己写一个 my.json { ...

  6. Android——excise(用线性布局、表格布局、相对布局做发送邮件界面)

    LinearLayout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...

  7. 【安卓】自己定义基于onDraw的随意动画(不不过平移/旋转/缩放/alpha)、!

    思路: 1.基于时间的显示映射.如:给定度数,显示圆弧,加上时序,就可以有圆弧动画的效果 2.给定时序. 用于驱动动画的一帧帧绘制 方案一.基于ObjectAnimator.动画运作时会调用degre ...

  8. 修改多渠道打包的App名

    archiveNameFormat = '${flavorName}-${projectName}-${versionName}-${versionCode}'

  9. 原生Orcale数据库连接

    package tj.test.demo; import java.sql.Connection;import java.sql.DriverManager;import java.sql.Prepa ...

  10. 求逆元 - HNU 13412 Cookie Counter

    Cookie Counter Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13412&a ...