问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. Python实现性能自动化测试竟然如此简单【颠覆你的三观】

    一.思考 1.什么是性能自动化测试?   性能系统负载能力 超负荷运行下的稳定性 系统瓶颈 自动化测试使用程序代替手工 提升测试效率 性能自动化使用代码模拟大批量用户 让用户并发请求 多页面多用户并发 ...

  2. Mesos+Zookeeper+Marathon+Docker环境搭建

    相关理论请参考:https://www.cnblogs.com/Bourbon-tian/p/7155054.html,本文基于https://www.cnblogs.com/Bourbon-tian ...

  3. Security and Risk Management(5)

    Ethics: ISC Code of Ethics You agree to this before the exam, and the code of ethics is very testabl ...

  4. GPO - Folder Mapping via GPO

    Create a Group Policy on AD DC Server. The GPO policy will come into effect on the next login, or us ...

  5. Python 简明教程 --- 26,Python 多进程编程

    微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 学编程最有效的方法是动手敲代码. 目录 1,什么是多进程 我们所写的Python 代码就是一个程序, ...

  6. js原声代码 轮播图

    js轮播图 html部分:建立div,内嵌img标签,可以设置大小, <!doctype html> <html> <head> <meta charset= ...

  7. A - A Simple Problem with Integers (线段树的区间修改与区间查询)

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

  8. PHP array_diff() 函数

    实例 比较两个数组的值,并返回差集: <?php $a1=array("a"=>"red","b"=>"gree ...

  9. PHP date_diff() 函数

    ------------恢复内容开始------------ 实例 计算两个日期间的差值: <?php$date1=date_create("2013-03-15");$da ...

  10. PHP usleep() 函数

    实例 延迟执行当前脚本 5 秒(5000000 微秒):高佣联盟 www.cgewang.com <?php echo date('h:i:s') . "<br>" ...