构建乘积数组,正则表达式匹配,表示数值的字符串,字符流中第一个不重复的字符(剑指offer51-54)c++版本

#include 
#include 
#include 

using namespace std;

class Solution {
     
public:
	//JZ51 构建乘积数组
	vector<int> multiply(const vector<int>& A);
	//JZ52 正则表达式匹配
	bool match(char* str, char* pattern);
	//JZ53 表示数值的字符串
	bool isNumeric(char* string);
	bool isnumbers(char* &string);
	//JZ54字符流中第一个不重复的字符	
	void Insert(char ch);//Insert one char from stringstream	
	char FirstAppearingOnce();//return the first appearence once char in current stringstream
private:
	//JZ54
	map<char, int> maps;
	string str;
};

//JZ51 构建乘积数组
vector<int> Solution::multiply(const vector<int>& A) {
     
	//参见剑指offer
	int len = A.size();
	if (len == 0 || len == 1)	return vector<int>();
	vector<int> result(len);
	result[0] = 1;
	for (int i = 1; i < len; i++) {
     
		result[i] = result[i - 1] * A[i - 1];
	}
	int temp = 1;
	for (int i = len - 2; i >= 0; i--) {
     
		temp = temp * A[i + 1];
		result[i] *= temp;
	}
	return result;
}
//JZ52 正则表达式匹配
bool Solution::match(char* str, char* pattern) {
     
	if (!pattern || !str)	return false;
	if (*str == '\0' && *pattern == '\0')	return true;
	if (*pattern == '\0')	return false;
	if (*(pattern + 1) == '*') {
     
		//pattern:.*
		if (*pattern == *str || (*str != '\0' && *pattern == '.'))
			return match(str + 1, pattern) || match(str, pattern + 2);
		else {
     
			return match(str, pattern + 2);
		}
	}
	if (*str != '\0' && (*pattern == *str ||  *pattern == '.'))
		return match(str + 1, pattern + 1);
	return false;
}
//JZ53 表示数值的字符串
bool Solution::isNumeric(char* string) {
     
	//数值的科学计数法:+-数字串.数字串e/E+-数字串 。注意,.3表示0.3
	if (!string || *string == '\0')	return false;
	if (*string == '+' || *string == '-')	string++;
	isnumbers(string);
	if (*string != '\0' && *string != 'e' && *string != 'E' && *string != '.')
		return false;
	if (*string == '.') {
     
		string++;
		if (isnumbers(string))	return false;
		if (*string != '\0' && *string != 'e' && *string != 'E')
			return	false;
	}
	if (*string == 'e' || *string == 'E') {
     
		++string;
		if (*string == '+' || *string == '-')
			string++;
		if (isnumbers(string))	return false;
	}
	if (*string == '\0')	return true;
	return false;
}
bool Solution::isnumbers(char* &string) {
     
	//找到第一个不是数字的位置
	char* temp = string;
	while (*string != '\0' && (*string >= '0' && *string <= '9'))
		string++;
	return temp == string;
}
//JZ54字符流中第一个不重复的字符	
void Solution::Insert(char ch) {
     
	//Insert one char from stringstream	
	if (!ch)	return;
	str.push_back(ch);
	maps[ch]++;
	return;
}
char Solution::FirstAppearingOnce() {
     
	//return the first appearence once char in current stringstream
	int len = str.size();
	for (int i = 0; i < len; i++) {
     
		if (maps[str[i]] == 1)	return str[i];
	}
	return '#';
}

//JZ51 构建乘积数组
void test1() {
     
	const vector<int> A = {
      1,2,3,4 };
	Solution s;
	vector<int> result = s.multiply(A);
	for (vector<int>::iterator it = result.begin(); it != result.end(); it++)
		cout << *it << ' ';
	return;
}
//JZ52 正则表达式匹配
void test2() {
     
	char* str = ",";
	char* pattern = ".*";
	Solution s;
	cout << s.match(str, pattern);
	return;
}
//JZ53 表示数值的字符串
void test3() {
     
	char* string = new char();
	string = "12e";
	Solution s;
	cout << s.isNumeric(string) << endl;
	cout << *string;
	//delete[]string;
	return;
}
//JZ54字符流中第一个不重复的字符
void test4() {
     
	Solution s;
	s.Insert('g');
	s.Insert('o');
	cout << s.FirstAppearingOnce();
	return;
}


int main() {
     
	//test1();
	//test2();
	//test3();
	test4();
	system("pause");
	return 0;
}

你可能感兴趣的:(剑指offer,算法,算法,c++)