leetCode 125. Valid Palindrome 字符串

简介:

125. Valid Palindrome


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

For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.

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

题目大意:

回文的检测。

思路:

1.清洗字符串,得到只有数字和字母的字符串。

2.通过比较首尾的字符来判断。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class  Solution {
public :
     vector<string> stringSplit(string s,  const  char  * split)
     {
         vector<string> result;
         const  int  sLen = s.length();
         char  *cs =  new  char [sLen + 1];
         strcpy (cs, s.data());
         char  *p;
     
         p =  strtok (cs, split);
         while  (p)
         {
             printf ( "%s\n" , p);
             string tmp(p);
             result.push_back(tmp);
             p =  strtok (NULL, split);
         }
         return  result;
     }
     bool  isPalindrome(string s) {
         if  (s.size() == 0 || s.size() == 1)
             return  true ;
         vector<string> vecStrs = stringSplit(s, " ~!@#$%^&*().,:;-?\"'`" );
         s =  "" ;
         for  ( int  i = 0; i < vecStrs.size(); i++)
             s += vecStrs[i];
         if  (s.size() == 1 || s.size() == 0)
             return  true ;
         int  i = 0;
         for  (; i < s.size() / 2; i++)
         {
             if  (s[i] <= 57 ||  s[s.size() - i - 1] <= 57)
             {
                 if  (s[i] == s[s.size() - i - 1])
                 {
                     continue ;
                 }
                 else
                 {
                     return  false ;
                 }
             }
             else  if  (s[i] == s[s.size() - i - 1] ||
                 s[i] - s[s.size() - i - 1] == 32 ||
                 s[s.size() - i - 1] - s[i] == 32)
             {
                 continue ;
             }
             else
             {
                 return  false ;
             }
         }
         return  true ;
     }
};


上面的做法效率低下,还有对API不熟悉。

下面是对上面的改进:

参考https://discuss.leetcode.com/topic/48376/12ms-c-clean-solution

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class  Solution {
public :
     bool  isPalindrome(string s) {
         int  i = 0, j = s.size() - 1;
         while  (i < j)
         {
             while  (! isalnum (s[i]) && i < j) i++;
             while  (! isalnum (s[j]) && i < j) j--;
             if  ( tolower (s[i++]) !=  tolower (s[j--]))
                 return  false ;
         }
         return  true ;
     }
};

这里使用了isalnum()函数来判断是否为文字数字。

通过使用tolower()来统一字符的大小写,都变为小写。



本文转自313119992 51CTO博客,原文链接:http://blog.51cto.com/qiaopeng688/1836839

相关文章
|
5月前
|
Go 索引
【LeetCode 热题100】394:字符串解码(详细解析)(Go语言版)
本文详细解析了 LeetCode 热题 394:字符串解码。题目要求对编码字符串如 `k[encoded_string]` 进行解码,其中 `encoded_string` 需重复 `k` 次。文章提供了两种解法:使用栈模拟和递归 DFS,并附有 Go 语言实现代码。栈解法通过数字栈与字符串栈记录状态,适合迭代;递归解法则利用函数调用处理嵌套结构,代码更简洁。两者时间复杂度均为 O(n),但递归需注意栈深度问题。文章还总结了解题注意事项及适用场景,帮助读者更好地掌握字符串嵌套解析技巧。
141 6
|
6月前
|
存储 机器学习/深度学习 缓存
🚀 力扣热题 394:字符串解码(详细解析)(Go语言版)
文章提供了两种解法:栈结构和递归解法。栈解法通过维护数字栈与字符串栈,依次处理 `[` 和 `]`,构造解码结果;递归解法则利用函数调用逐层解析嵌套结构。两者时间复杂度均为 $O(n)$,空间复杂度也为 $O(n)$。栈解法直观易懂,适合初学者;递归解法优雅简洁,适合处理深度嵌套规则。掌握这两种方法,可灵活应对类似问题,提升解题能力。
188 11
|
11月前
|
JavaScript
力扣3333.找到初始输入字符串Ⅱ
【10月更文挑战第9天】力扣3333.找到初始输入字符串Ⅱ
114 1
|
11月前
|
C++
Leetcode第43题(字符串相乘)
本篇介绍了一种用C++实现的字符串表示的非负整数相乘的方法,通过逆向编号字符串,将乘法运算转化为二维数组的累加过程,最后处理进位并转换为字符串结果,解决了两个大数相乘的问题。
76 9
|
11月前
|
算法 C++
Leetcode第八题(字符串转换整数(atoi))
这篇文章介绍了LeetCode上第8题“字符串转换整数(atoi)”的解题思路和C++的实现方法,包括处理前导空格、正负号、连续数字字符以及整数溢出的情况。
124 0
|
11月前
【LeetCode 22】459.重复的子字符串
【LeetCode 22】459.重复的子字符串
93 0
|
11月前
【LeetCode 20】151.反转字符串里的单词
【LeetCode 20】151.反转字符串里的单词
74 0
|
11月前
【LeetCode 19】541.反转字符串II
【LeetCode 19】541.反转字符串II
60 0
|
11月前
【LeetCode 18】6.2.反转字符串
【LeetCode 18】6.2.反转字符串
63 0
|
存储 算法
LeetCode第43题字符串相乘
LeetCode第43题"字符串相乘"的解题方法,通过使用数组存储乘积并处理进位,避免了字符串转换数字的复杂性,提高了算法效率。
LeetCode第43题字符串相乘