问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3899 访问。

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

说明:本题中,我们将空字符串定义为有效的回文串。

输入: "A man, a plan, a canal: Panama"

输出: true

输入: "race a car"

输出: false


Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

Input: "A man, a plan, a canal: Panama"

Output: true

Input: "race a car"

Output: false


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3899 访问。

public class Program {

    public static void Main(string[] args) {
var s = "0P"; var res = IsPalindrome(s);
Console.WriteLine(res); s = "A man, a plan, a canal: Panama"; res = IsPalindrome2(s);
Console.WriteLine(res); Console.ReadKey();
} private static bool IsPalindrome(string s) {
//基本思路,反转字符串后判定和之前是否相同
//LeetCode超时未AC
s = FilterCharacter(s);
var reverse = "";
s.Reverse().ToList().ForEach(c => reverse += c);
return s == reverse;
} /// <summary>
/// 过滤非字母以外的字符串
/// </summary>
/// <returns>过滤后的字符串</returns>
/// <param name="s">待过滤的字符串</param>
private static string FilterCharacter(string s) {
var res = string.Empty;
s = s.ToLower().Trim();
s.ToList().ForEach(c => {
if((c >= 48 && c <= 57) ||
(c >= 97 && c <= 122)) res += c;
});
return res;
} private static bool IsPalindrome2(string s) {
//基本思路,先用List存放所有符合条件的字符
//再比较前半部分和后半部分
s = s.ToLower().Trim();
var list = new List<char>();
foreach(var c in s) {
if((c >= 48 && c <= 57) ||
(c >= 97 && c <= 122)) {
list.Add(c);
}
}
var mid = list.Count / 2;
for(var i = 0; i < mid; i++) {
if(list[i] != list[list.Count - i - 1]) return false;
}
return true;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3899 访问。

False
True

分析:

显而易见,以上2种算法的时间复杂度均为:  。

需要注意的是,虽然以上2种算法的时间复杂度相同,但是 IsPalindrome2 的执行效率明显更高,因为只有1.5次遍历,并且比较部分使用了效率较高的数据结构 List;IsPalindrome 有反转字符串和去除不合法字符的代码,所以至少需要遍历2次。IsPalindrome2 是典型的空间换时间算法。

C#LeetCode刷题之#125-验证回文串(Valid Palindrome)的更多相关文章

  1. leetcode 125 验证回文字符串 Valid Palindrome

    验证回文字符串 C++ 思路就是先重新定义一个string ,先遍历第一遍,字符串统一小写,去除空格:然后遍历第二遍,首尾一一对应比较:时间复杂度O(n+n/2),空间O(n); class Solu ...

  2. C#LeetCode刷题之#409-最长回文串(Longest Palindrome)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3788 访问. 给定一个包含大写字母和小写字母的字符串,找到通过这 ...

  3. leetcode刷题1--动态规划法回文串2

    题目是: Given a string s,partition s such that every substring of the partition is a palindrome Return ...

  4. [Swift]LeetCode125. 验证回文串 | Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. 前端与算法 leetcode 125. 验证回文串

    目录 # 前端与算法 leetcode 125. 验证回文串 题目描述 概要 提示 解析 解法一:api侠 解法二:双指针 算法 传入测试用例的运行结果 执行结果 GitHub仓库 查看更多 # 前端 ...

  6. Java实现 LeetCode 125 验证回文串

    125. 验证回文串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...

  7. leetcode刷题五<最长回文子串>

    下面是题目的描述 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 . 示例 : 输入: "babad" 输出: "bab" 注意: ...

  8. 力扣(LeetCode)125. 验证回文串

    给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a c ...

  9. leetcode 125. 验证回文串(python)

    给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a c ...

随机推荐

  1. 通过hmail搭建一个内网测试的邮件服务器

    ​    我们测试的软件基本上都是支持邮件功能,如果你的测试环境是在外网的话那还好说,可以直接使用QQ邮箱.163邮箱等.但是如果是测试环境在内网,无法直接访问到外网的时候,搭建一个邮件服务器就很有必 ...

  2. Linux下显示运行时链接(运行时加载)

    目录 介绍 如何加载动态库 dlopen() 第一个参数: 被加载动态库的路径 第二个参数: flag表示函数符号的解析方式 dlopen 返回值 dlsym() 参数: 返回值 符号优先级 dler ...

  3. pyenv虚拟环境安装

    安装过程 配置yum源 # curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo ...

  4. Oracle对表进行备份

    前言: 在实际开发中,我们常常需要对单张或多张表进行备份,以下博主就从这两个方面进行总结.如需转载,请标明来处,谢谢! 在备份前我们先创建表盒相关测试的数据 -- Create table creat ...

  5. 毫无基础的人入门Python,Python新手入门教程2

    1.6 面向对象和内存分析086.面向对象和面向过程的区别_执行者思维_设计者思维087.对象的进化故事088.类的定义_类和对象的关系089.构造函数__init__090.实例属性_内存分析091 ...

  6. 使用Openresty构建认证网关

    [入门]使用Openresty构建认证网关 lwhile关注 0.5092017.10.07 16:00:03字数 1,330阅读 4,112 在单体应用中, 我们可以通过 cookie + sess ...

  7. ken桑带你读源码 之scrapy scrapy\extensions

    logstats.py 爬虫启动时 打印抓取网页数   item数 memdebug.py 爬虫结束 统计还被引用的内存 也就是说gc 回收不了的内存   memusage.py 监控爬虫 内存占用  ...

  8. 选择排序的实现以及如何编写测试 #CS61B-sp18-3.1

    Selection Sort的思想: 就是在一系列数字中先找到一个最小的放在所有数字的第一个位置上,然后再从余下的数字里面找最小个放在余下的数字里的第一个位置上. 例如: 在这段数据里面我们找到最小的 ...

  9. VuePress博客美化之reco主题

    vuepress博客主题-vuepress-theme-reco是一款简洁而优雅的 vuepress博客&文档主题.它既可以成为简洁而又不失美观的主题,又可以书写你的项目文档,看起来更有逼格. ...

  10. 无法安装 VMware Tools。尝试访问安装 VMware Tools 所需的图像文件“/usr/lib/vmware/isoimages/linuxPreGlibc25.iso”时出错: 2 (No such file or directory)。请参考产品文档或知识库文章 2129825,了解关于如何获取该客户机操作系统的 VMware Tools 软件包的详细信息。

    无法安装 VMware Tools.尝试访问安装 VMware Tools 所需的图像文件"/usr/lib/vmware/isoimages/linuxPreGlibc25.iso&quo ...