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. Ubuntu环境下使用npm安装node模块时报错的处理方法

    错误信息: npm ERR : node: not found : npm ERR! not ok code 0 解决方案: sudo apt-get install nodejs-legacy 也可 ...

  2. iOS应用安全防护框架概述

    iOS应用安全防护框架概述 攻易防难,唯有缜密.多层的防护网络才能可靠的保护我们iOS应用程序的安全.那么,一个完善的iOS应用安全防护框架都要写哪些东西呢? 首先,先梳理一下常见的逆向及攻击工具. ...

  3. 使用ReaderWriterLock类实现多用户读/单用户写同步

    使用ReaderWriterLock类实现多用户读/单用户写同步[1] 2015-03-12 应用程序在访问资源时是进行读操作,写操作相对较少.为解决这一问题,C#提供了System.Threadin ...

  4. private继承的作用

    这里有个demo,里面的Stack<T*> : private Stack<void *>,作者对此的解释如下 The partial specialization for o ...

  5. js代码中定义后台java中的上下文路径

    var href = <c:out value='${sessionScope.contextPath}' />

  6. CSS学习笔记(4)--选择器(w3school)

    CSS3 选择器 在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素. "CSS" 列指示该属性是在哪个 CSS 版本中定义的.(CSS1.CSS2 还是 CSS3.) ...

  7. python操作word(改课文格式)【最终版】

    python操作word的一些方法,前面写了一些感悟,有点跑题,改了下题目,方便能搜索到.心急的可以直接拉到最后看代码,我都加了比较详细的注释. 从8.3号早上9点,到8.8号下午5点半下班,终于把这 ...

  8. Linux下protobuf的编译与安装

    1.下载源码 首先,从github上下载protobuf的源码,地址:https://github.com/google/protobuf,我选择下载2.5.0版本. 2.编译protobuf 将下载 ...

  9. MapReduce程序的工作过程

    转自:http://www.aboutyun.com/thread-15494-1-2.html 问题导读1.HDFS框架组成是什么?2.HDFS文件的读写过程是什么?3.MapReduce框架组成是 ...

  10. android从放弃到坚持放弃第二课(下)

    续第二课( 下) 续第二课 下 活动的生命周期 返回栈 活动状态 活动的生存期 体验活动的生命周期 活动被回收怎么办 活动的启动模式 standard singleTop singleTask sin ...