Leetcode: Valid Palindrome

简单模拟

 

代码:

#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
	bool isAlpha(char &in) {
		if(in >= 'a' && in <= 'z')
			return true;
		else if(in >= 'A' && in <= 'Z') {
			in = in-'A'+'a';
			return true;
		}else if(in >= '0' && in <= '9')
			return true;
		return false;
	}
    bool isPalindrome(string s) {

		int l = 0, h = s.size()-1;
		bool ans = true;
		while(l < h) {
			char cl, cr;
			cl = s[l];
			while(!isAlpha(cl) && l < h) {
				l++;
				cl = s[l];
			}
			if(l == h)
				return true;
			cr = s[h];
			while(!isAlpha(cr) && l < h) {
				h--;
				cr = s[h];
			}
			if(l == h)
				return true;
			if(cl != cr)
				return false;
			else {
				l++;
				h--;
			}
		}
		return ans;
    }
};

int main() {
	Solution s;
	cout << s.isPalindrome("0k.;r0.k;") << endl;
	return 0;
}

  

posted @ 2013-11-28 22:58  SangS  阅读(158)  评论(0编辑  收藏  举报