问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章

  1. #leetcode刷题之路9- 回文数

    判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1:输入: 121输出: true 示例 2:输入: -121输出: false解释: 从左向右读, 为 ...

  2. Leetcode 9 回文数Palindrome Number

    判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...

  3. [Swift]LeetCode9. 回文数 | Palindrome Number

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

  4. Leetcode题库——9.回文数

    @author: ZZQ @software: PyCharm @file: HuiWenShu.py @time: 2018/9/16 16:51 要求:判断一个整数是否是回文数.回文数是指正序(从 ...

  5. Java实现 LeetCode 564 寻找最近的回文数(今天要GG在这道题了 头晕+题难(((φ(◎ロ◎;)φ))))

    564. 寻找最近的回文数 给定一个整数 n ,你需要找到与它最近的回文数(不包括自身). "最近的"定义为两个整数差的绝对值最小. 示例 1: 输入: "123&quo ...

  6. LeetCode(9):回文数

    Easy! 题目描述: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: f ...

  7. [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 ...

  8. 【leetcode算法-简单】9. 回文数

    [题目描述] 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121输出: true示例 2: 输入: -121输出: false解释: ...

  9. Leetcode 564.寻找最近的回文数

    寻找最近的回文数 给定一个整数 n ,你需要找到与它最近的回文数(不包括自身). "最近的"定义为两个整数差的绝对值最小. 示例 1: 输入: "123" 输出 ...

  10. [2014亚马逊amazon] 在线笔试题 大于非负整数N的第一个回文数 Symmetric Number

    1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 这个题目也是当时笔试第一次见到,花了一个小时才做出了.慢慢总结还是挺简单的. 2.分析 分析如下: (1)一位数N(9 ...

随机推荐

  1. bootstrap中模态框如果放入form表单中会存在的问题

    bootstrap中模态框如果放入form表单中会存在的问题:当模态框显示时,点回车会出现表单自动提交!!!所以在使用模态框的时候要特别注意!

  2. 【Nginx】如何格式化日志并推送到远程服务器?看完原来很简单!!

    写在前面 Nginx作为最常用的反向代理和负载均衡服务器,被广泛的应用在众多互联网项目的前置服务中,很多互联网项目直接将Nginx服务器作为整个项目的流量入口.这就使得我们可以通过对Nginx服务器日 ...

  3. Go Pentester - HTTP Servers(2)

    Routing with the gorilla/mux Package A powerful HTTP router and URL matcher for building Go web serv ...

  4. OSCP Learning Notes - Exploit(7)

    Pre-Exploit Password Attacks Tools: 1. ncrack Ncrack 0.6 ( http://ncrack.org )Usage: ncrack [Options ...

  5. 前端学习(一):Html

    进击のpython ***** 前端学习--HTML HTML全称HyperText Mackeup Language,超文本标记语言 网页的超链接,图片,音频,视频都可以超文本 标记就相当于你在本子 ...

  6. 我自己总结的sqlite的命令行命令集

    我自己总结的sqlite 的命令行命令 导入文本数据文件时,设置分隔符为","sql>.separator "," sql>import devic ...

  7. C# 13位时间戳(unix时间戳)

    1.转义字符用在中间. "\"' 2.C#获取13位时间戳(unix时间戳) /// <summary>   /// 将c# DateTime时间格式转换为Unix时间 ...

  8. .NET Core学习笔记(7)——Exception最佳实践

    1.为什么不要给每个方法都写try catch 为每个方法都编写try catch是错误的做法,理由如下: a.重复嵌套的try catch是无用的,多余的. 这一点非常容易理解,下面的示例代码中,O ...

  9. Oracle帐户被锁了,如何解锁

    原文链接:https://jingyan.baidu.com/article/25648fc144b76b9191fd0087.html 背景:Oracle帐户在密码被连续输入错误3次的情况下就会锁定 ...

  10. PHP fread() 函数

    定义和用法 fread() 函数读取打开的文件. 函数会在到达指定长度或读到文件末尾(EOF)时(以先到者为准),停止运行. 该函数返回读取的字符串,如果失败则返回 FALSE. 语法 string ...