leetcode 125

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 class Solution {
 2 public:
 3     bool isPalindrome(string s) {
 4         int n = s.size();
 5         int st = 0;
 6         if(n == 0)
 7         {
 8             return true;
 9         }
10         n--;
11         while(st < n)
12         {
13             if(isValid(s[st]) && isValid(s[n]))
14             {
15                 if(s[st] <= 57 && s[st] >= 48)
16                 {
17                     if(s[n] > 57 || s[n] < 48)
18                     {
19
20                         return false;
21                     }
22                     else if(s[st] != s[n])
23                     {
24                         return false;
25                     }
26                     else
27                     {
28                         st++;
29                         n--;
30                         continue;
31                     }
32                 }
33                 if(s[st] == s[n] || s[st]-s[n] == 32 || s[n]-s[st] == 32)
34                 {
35                     st++;
36                     n--;
37                     continue;
38                 }
39                 else
40                 {
41                     return false;
42                 }
43             }
44             else if(isValid(s[st]))
45             {
46                 n--;
47             }
48             else if(isValid(s[n]))
49             {
50                 st++;
51             }
52             else
53             {
54                 n--;
55                 st++;
56             }
57         }
58         return true;
59     }
60     bool isValid(char s)
61     {
62         if(s < 48 || s > 122)
63         {
64             return false;
65         }
66         else if(s > 57 && s < 65)
67         {
68             return false;
69         }
70         else if(s > 90 && s < 97)
71         {
72             return false;
73         }
74         return true;
75     }
76 };
时间: 2024-10-14 05:00:26

leetcode 125的相关文章

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

目录 # 前端与算法 leetcode 125. 验证回文串 题目描述 概要 提示 解析 解法一:api侠 解法二:双指针 算法 传入测试用例的运行结果 执行结果 GitHub仓库 查看更多 # 前端与算法 leetcode 125. 验证回文串 题目描述 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a canal: Panama" 输出: tru

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:Hav

LeetCode(125)题解--Valid Palindrome

https://leetcode.com/problems/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"

[LeetCode] 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 th

leetcode 125. Valid Palindrome

题目描述 c语言中的tolowe(A)函数,将字符A转换为小写: isalpha(a) 判断a是不是一个字符 isdigital(a)判断a是不是一个数字,isalnum(a)判断a是不是一个数字或者字符 只判断字符串中字符是不是回文 空字符串是一个回文字符串 class Solution { public: bool isPalindrome(string s) { int i = 0; int j = s.size() -1; while(i < j){ while(!isalnum(s[i

Java [Leetcode 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 t

leetcode 125. Valid Palindrome ----- java

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 th

Java for LeetCode 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. 解题思路: 注意题目,忽略大小写,忽略非字母或数字,JA

[C++]LeetCode: 125 Sort List (归并排序链表)

题目:Sort a linked list in O(n log n) time using constant space complexity. 思路:题目要求我们使用常数空间复杂度,时间复杂度为O(nlog(n)). 满足这个时间复杂度的有快速排序,归并排序,堆排序.插入排序时间复杂度为O(n^2). 双向链表用快排比较合适,堆排序也可用于链表,单项链表适合于归并排序.我们就用归并排序的思想来完成链表的排序. 首先是用快慢双指针找到链表中间的位置,然后分成前后端分别递归的归并排序,最后合并.