C#LeetCode刷题之#9-回文数(Palindrome Number)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3840 访问。
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
输入: 121
输出: true
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
进阶:
你能不将整数转为字符串来解决这个问题吗?
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Input: 121
Output: true
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3840 访问。
public class Program {
public static void Main(string[] args) {
var x = 121;
var res = IsPalindrome(x);
Console.WriteLine(res);
x = -567;
res = IsPalindrome2(x);
Console.WriteLine(res);
x = 168861;
res = IsPalindrome3(x);
Console.WriteLine(res);
Console.ReadKey();
}
private static bool IsPalindrome(int x) {
//计算反转值
if(x < 0) return false;
var res = 0L;
var value = x;
while(x != 0) {
res = res * 10 + x % 10;
x = x / 10;
}
return res == value;
}
private static bool IsPalindrome2(int x) {
//反转字符串
if(x < 0) return false;
var arr = x.ToString().ToCharArray();
Array.Reverse(arr);
return x.ToString() == new string(arr);
}
private static bool IsPalindrome3(int x) {
//栈
if(x < 0) return false;
var palindrome = x.ToString();
var stack = new Stack<char>();
for(var i = 0; i < palindrome.Length / 2; i++) {
stack.Push(palindrome[i]);
}
//奇数时,往后移一位
int pos = 0;
if(palindrome.Length % 2 == 1) {
pos = 1;
}
for(var i = palindrome.Length / 2 + pos; i < palindrome.Length; i++) {
if(palindrome[i] != stack.Pop()) return false;
}
return true;
}
}
以上给出3种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3840 访问。
True
False
True
分析:
显而易见,以上3种算法的时间复杂度均为: 。
C#LeetCode刷题之#9-回文数(Palindrome Number)的更多相关文章
- #leetcode刷题之路9- 回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1:输入: 121输出: true 示例 2:输入: -121输出: false解释: 从左向右读, 为 ...
- Leetcode 9 回文数Palindrome Number
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- [Swift]LeetCode9. 回文数 | Palindrome Number
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- Leetcode题库——9.回文数
@author: ZZQ @software: PyCharm @file: HuiWenShu.py @time: 2018/9/16 16:51 要求:判断一个整数是否是回文数.回文数是指正序(从 ...
- Java实现 LeetCode 564 寻找最近的回文数(今天要GG在这道题了 头晕+题难(((φ(◎ロ◎;)φ))))
564. 寻找最近的回文数 给定一个整数 n ,你需要找到与它最近的回文数(不包括自身). "最近的"定义为两个整数差的绝对值最小. 示例 1: 输入: "123&quo ...
- LeetCode(9):回文数
Easy! 题目描述: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: f ...
- [LeetCode] 906. Super Palindromes 超级回文数
Let's say a positive integer is a superpalindrome if it is a palindrome, and it is also the square o ...
- 【leetcode算法-简单】9. 回文数
[题目描述] 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121输出: true示例 2: 输入: -121输出: false解释: ...
- Leetcode 564.寻找最近的回文数
寻找最近的回文数 给定一个整数 n ,你需要找到与它最近的回文数(不包括自身). "最近的"定义为两个整数差的绝对值最小. 示例 1: 输入: "123" 输出 ...
- [2014亚马逊amazon] 在线笔试题 大于非负整数N的第一个回文数 Symmetric Number
1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 这个题目也是当时笔试第一次见到,花了一个小时才做出了.慢慢总结还是挺简单的. 2.分析 分析如下: (1)一位数N(9 ...
随机推荐
- (6)webpack使用babel插件的使用
为什么要使用babel插件? 首先要了解babel插件是干嘛的,随着js的语法规范发展,出现了越来越多的高级语法,但是使用webpack打包的时候,webpack并不能全部理解这些高级语法,需要我们使 ...
- Python Ethical Hacking - Malware Analysis(4)
DOWNLOAD_FILE Download files on a system. Once packaged properly will work on all operating systems. ...
- 重学数据结构(三)——使用单链表实现LRU淘汰缓存机制
使用单链表实现LRU(Least Recently Used)淘汰缓存机制 需求:存在一个单链表,在单链表尾部的都是越早之前添加的元素. 当元素被访问到时,会添加进缓存(也就是这个单链表中). 如果这 ...
- js读取其他网页内容(同源)
通过xss第一次取得网页内容,然后获取到管理员账号页面进行二次盲打.js需要保留script部分其余去除. <html><p id='d1'></p> <sc ...
- vue学习(十三) 删除对象数组中的某个元素
//html <div id="app"> //v-for循环就不写了 每一条数据最后都有一个删除的超链 .prevent阻止默认的跳转行为 只执行点击事件 <a ...
- Windows 平台做 Python 开发的最佳组合
在 Windows 上怎样做 Python 开发?是像大神那样使用纯文本编辑器,还是用更加完善的 IDE?到底是用自带的命令行工具,还是需要装新的 Terminal?本文将带你了解如何利用微软官方维护 ...
- anaconda一站式环境的搭建(anaconda、tensorflow、opencv)
搭建人工智能图像处理环境 Anaconda一站式开发环境搭建. 工欲善其事必先利其器,在我们学习之前,我们先要搭建一个属于我们自己的开发环境.我们开发的环境是有anaconda.testflow.op ...
- PHP cal_days_in_month() 函数
------------恢复内容开始------------ 实例 针对指定的年份和历法,获取一个月中的天数: <?php$d=cal_days_in_month(CAL_GREGORIAN,1 ...
- PHP is_readable() 函数
定义和用法 is_readable() 函数检查指定的文件是否可读. 如果文件可读,该函数返回 TRUE. 语法 is_readable(file) 参数 描述 file 必需.规定要检查的文件. 提 ...
- PHP tempnam() 函数
定义和用法 tempnam() 函数在指定的目录中创建一个具有唯一文件名的临时文件. 该函数返回新的临时文件名,如果失败则返回 FALSE. 语法 tempnam(dir,prefix) 参数 描述 ...