Created
August 19, 2016 22:25
-
-
Save jianminchen/1e1724c79805d2f40195f08135ec4d02 to your computer and use it in GitHub Desktop.
Leetcode 125 - valid palindrome - second writing, through online judge, line 24 -27, else if logic has flaws - c1 not, c2 not, 3 cases, c1, or c2, or neither
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public bool IsPalindrome(string s) { | |
if (s == null || s.Length == 0) | |
return true; | |
int left = 0; | |
int right = s.Length - 1; | |
while (left < right) | |
{ | |
char c1 = s[left]; | |
char c2 = s[right]; | |
if (isAlnum(c1) && isAlnum(c2)) | |
{ | |
if (toUpper(c1) == toUpper(c2)) | |
{ | |
left++; | |
right--; | |
} | |
else | |
return false; | |
} | |
else if (!isAlnum(c1)) | |
left++; | |
else | |
right--; | |
} | |
return true; | |
} | |
/* | |
* a-z | |
* A-Z | |
* 0-9 | |
*/ | |
private bool isAlnum(char c) | |
{ | |
const int SIZE = 26; | |
int[] arr = new int[] { c - 'a', c - 'A', c - '0' }; | |
// is A-Z or a-z or 0 -9 | |
if((arr[0] >= 0 && arr[0] < SIZE) || | |
(arr[1] >= 0 && arr[1] < SIZE) || | |
(arr[2] >= 0 && arr[2] <= 9 ) ) | |
return true; | |
return false; | |
} | |
/* | |
* | |
*/ | |
private char toUpper(char c) | |
{ | |
int no = c - 'a'; | |
if (no >= 0 && no < 26) | |
return (char)('A' + no); | |
else | |
return c; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
highlights of practice 2:
both are ok
both are failed
A is failed
B is failed
Those are 4 cases - in the specific order as well.