问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. 帮助你更好的理解Spring循环依赖

    网上关于Spring循环依赖的博客太多了,有很多都分析的很深入,写的很用心,甚至还画了时序图.流程图帮助读者理解,我看了后,感觉自己是懂了,但是闭上眼睛,总觉得还没有完全理解,总觉得还有一两个坎过不去 ...

  2. 在 vue 中使用 WebSocket

    <template> <div class="hello"> <h1>{{ msg }}</h1> <h1>{{ res ...

  3. vue+axios+springboot文件下载

    //前台代码 <el-button size="medium" type="primary" @click="downloadFile" ...

  4. flask中的endpoint是什么

    app.view_functions 是一个字典,里面是存储的是 endpoint 与 视图函数的键值对,如果没指名函数视图的endpoint,默认是函数名,而 url_map 是一个列表,里面是ur ...

  5. Java瞬态变量transient

      我们都知道,阳光是看得见却摸不着的.它真实的存在,但是却无法将其装在罐子里,这是因为光子不具有静止质量.这注定我们只能利用光子而不能将其捕获(或许只是暂时).在Java中,有一种变量就像光子一样, ...

  6. Python(set/list/dict/tuple)

    set集合:set是一个无序,不重复元素的集合.可嵌套列表,字典(可以for循环或者迭代的对象). ######差集: a={11,22} b={22,33} c=a.difference(b) #a ...

  7. python学习笔记1 -- 函数式编程之高阶函数 filter

    filter 函数用于过滤序列,与map 和reduce函数类似,作为高阶函数,他们也是同样的使用方法,filter(参数1, 参数2),参数1是一个函数,而参数2是一个序列. filter的作用是根 ...

  8. Fortify Audit Workbench 笔记 Privacy Violation 隐私泄露

    Privacy Violation 隐私泄露 Abstract 对各种机密信息处理不当,如客户密码或社会保障号码,会危及到用户的个人隐私,这是一种非法行为. Explanation Privacy V ...

  9. PHP fgetc() 函数

    定义和用法 fgetc() 函数从打开的文件中返回一个单一的字符. 语法 fgetc(file) 参数 描述 file 必需.规定要检查的文件. 提示和注释 注释:该函数处理大文件非常缓慢,所以它不用 ...

  10. 2020牛客暑假多校训练营 第二场 G Greater and Greater bitset

    LINK:Greater and Greater 确实没能想到做法. 考虑利用bitset解决问题. 做法是:逐位判断每一位是否合法 第一位 就是 bitset上所有大于\(b_1\)的位置 置为1. ...