C#LeetCode刷题之#125-验证回文串(Valid Palindrome)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- leetcode 125 验证回文字符串 Valid Palindrome
验证回文字符串 C++ 思路就是先重新定义一个string ,先遍历第一遍,字符串统一小写,去除空格:然后遍历第二遍,首尾一一对应比较:时间复杂度O(n+n/2),空间O(n); class Solu ...
- C#LeetCode刷题之#409-最长回文串(Longest Palindrome)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3788 访问. 给定一个包含大写字母和小写字母的字符串,找到通过这 ...
- leetcode刷题1--动态规划法回文串2
题目是: Given a string s,partition s such that every substring of the partition is a palindrome Return ...
- [Swift]LeetCode125. 验证回文串 | Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 前端与算法 leetcode 125. 验证回文串
目录 # 前端与算法 leetcode 125. 验证回文串 题目描述 概要 提示 解析 解法一:api侠 解法二:双指针 算法 传入测试用例的运行结果 执行结果 GitHub仓库 查看更多 # 前端 ...
- Java实现 LeetCode 125 验证回文串
125. 验证回文串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...
- leetcode刷题五<最长回文子串>
下面是题目的描述 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 . 示例 : 输入: "babad" 输出: "bab" 注意: ...
- 力扣(LeetCode)125. 验证回文串
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a c ...
- leetcode 125. 验证回文串(python)
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a c ...
随机推荐
- 三、Python系列——Pandas数据库读取数据
Pandas主要先读取表格类型的数据,然后进行分析. import pandas as pd# 由于是用pandas模块操作数据,因此不用在路径前加open,否则就是python直接打开文件,可能还会 ...
- 转自fineui论坛:解决fineui框架开发中的Designer.aspx.cs丢失问题
在开发的时候碰到个问题,本来好好的Edit.aspx Edit.aspx.cs Edit.Designer.aspx.cs编辑Edit.aspx然后保存,编译的时候 发现Edit.aspx.cs里 ...
- Ethical Hacking - NETWORK PENETRATION TESTING(9)
WEP Cracking Packet Injection What if the AP was idle, or had no clients associated with it? In this ...
- Python Ethical Hacking - BACKDOORS(5)
File Download: A file is a series of characters. Therefore to transfer a file we need to: 1. Read th ...
- try-catch- finally块中, finally块唯一不执行的情况是什么?
除非在try块或者catch块中调用了退出虚拟机的方法(即System.exit(1);),否则不管在try块.catch块中执行怎样的代码,出现怎样的情况,异常处理的finally块总是会被执行的 ...
- 使用SQL语句进行特定值排序
使用SQL语句进行查询时,对数据进行排序,排序要求为排序的一个字段中特定值为顶部呈现: select * from TableName order by case TableFieldName whe ...
- css盒子流动和block。inline
回忆一下盒子流动等概念! 1.盒子模型的宽度与高度,都是包括padding的值.(代码的理解如下:) 这样的结果的到就是 宽度和高度都是220了 2.流动型,在标签中存在块级元素和行内元素, 块级元 ...
- DOM事件操作
DOM事件:对事件做出反应 当事件发生时,可以执行 JavaScript,比如:点击时 onClick="" 例:当用户点击时,会改变 <h1> 元素的内容: < ...
- 恕我直言,我也是才知道ElasticSearch条件更新是这么玩的
背景 ElasticSearch 的使用度越来越普及了,很多公司都在使用.有做日志搜索的,有做商品搜索的,有做订单搜索的. 大部分使用场景都是通过程序定期去导入数据到 ElasticSearch 中, ...
- PHP array_unshift() 函数
实例 插入元素 "blue" 到数组中: <?php$a=array("a"=>"red","b"=> ...