Skip to content

Commit

Permalink
Complete No.19 and No.21 questions.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaojianfan committed Apr 9, 2023
1 parent 2f68211 commit 42d2bc5
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Algorithm Exercise of the book: *Coding Interviews: Questions, Analysis and Solutions*
37 changes: 35 additions & 2 deletions src/chapter-3/19_match_regular_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,43 @@
* 例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但与"aa.a"和"ab*a"均不匹配。
*/

bool solution::MatchRegularExpression(string str, string pattern) {
if(str== ""||pattern==""){
// NOT PASS
bool solution::MatchRegularExpression(string s, string p) {
if (s.empty() && p.empty()) {
return true;
}

if (!s.empty() && p.empty()) {
return false;
}

if (p[1] == '*') {
if (p[0] == s[0] || (p[0] == '.' && !s.empty())) {
// 完成匹配,进入下一状态
bool res1 = MatchRegularExpression(CutString(s, 1), CutString(p, 2));
// 保持当前状态
bool res2 = MatchRegularExpression(CutString(s, 1), p);
// 忽略*,进入下一状态
bool res3 = MatchRegularExpression(s, CutString(p, 2));

return res1 || res2 || res3;
} else {
// 忽略*,进入下一状态
bool res = MatchRegularExpression(s, CutString(p, 2));
return res;
}
}

if (p[0] == s[0] || (p[0] == '.' && !s.empty())) {
// 完成匹配,进入下一状态
bool res = MatchRegularExpression(CutString(s, 1), CutString(p, 1));
return res;
}

return false;
}

string solution::CutString(string& s, int n) {
string newStr;
return newStr.assign(s, n, s.length() - n);
}
36 changes: 36 additions & 0 deletions src/chapter-3/21_reorder_odd_even.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "solution.h"

/*
* 21. 调整数组顺序使奇数位于偶数前面
* 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数在数组的前半部分,所有偶数在数组的后半部分。
*/

vector<int> solution::Reorder(vector<int>& nums) {
if (nums.empty()) {
return nums;
}

auto pBegin = nums.begin();
auto pEnd = --nums.end();
auto IsOdd = [](int num) -> bool {
return num % 2 == 1;
};

while (pBegin < pEnd) {
while (pBegin < pEnd && IsOdd(*pBegin)) {
pBegin++;
}

while (pBegin < pEnd && !IsOdd(*pEnd)) {
pEnd--;
}

if (pBegin < pEnd) {
int temp = *pBegin;
*pBegin = *pEnd;
*pEnd = temp;
}
}

return nums;
}
7 changes: 6 additions & 1 deletion src/include/solution.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ ListNode* DeleteNode(ListNode* head, int val);
void DeleteDuplication(ListNode** pHead);

// Question 19
bool MatchRegularExpression(string str, string pattern);
bool MatchRegularExpression(string s, string p);

string CutString(string& s, int n);

// Question 20
bool IsNumber(string s);
Expand All @@ -100,4 +102,7 @@ string CutNumString(string& s);
bool ScanInteger(string& s);

bool ScanUnsignedInteger(string& s);

// Question 21
vector<int> Reorder(vector<int>& nums);
}
8 changes: 0 additions & 8 deletions src/include/util.h

This file was deleted.

4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ using namespace solution;
using namespace ds;

int main() {
std::string str = "";
std::string pattern = "";
bool res = solution::MatchRegularExpression(str, pattern);
std::cout << res << std::endl;

return 0;
}

0 comments on commit 42d2bc5

Please sign in to comment.