Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created August 19, 2016 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/1e1724c79805d2f40195f08135ec4d02 to your computer and use it in GitHub Desktop.
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
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;
}
}
@jianminchen
Copy link
Author

highlights of practice 2:

  1. Fail to pass the online judge
  2. line 24 - 27, logic and reasoning has flaws
    both are ok
    both are failed
    A is failed
    B is failed
    Those are 4 cases - in the specific order as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment