-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy path0125-ValidPalindrome.cs
35 lines (32 loc) · 1.03 KB
/
0125-ValidPalindrome.cs
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
//-----------------------------------------------------------------------------
// Runtime: 76ms
// Memory Usage: 23.6 MB
// Link: https://leetcode.com/submissions/detail/271483819/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _0125_ValidPalindrome
{
public bool IsPalindrome(string s)
{
if (string.IsNullOrWhiteSpace(s)) return true;
s = s.ToLower();
int head = 0, tail = s.Length - 1;
while (head <= tail)
{
if ((s[head] < 'a' || s[head] > 'z') && (s[head] < '0' || s[head] > '9'))
head++;
else if ((s[tail] < 'a' || s[tail] > 'z') && (s[tail] < '0' || s[tail] > '9'))
tail--;
else if (s[head] != s[tail])
return false;
else
{
head++;
tail--;
}
}
return true;
}
}
}