#include <iostream>
#include <vector>
using namespace std;
struct ListNode {
int val;
struct ListNode* next;
ListNode(int x) :val(x), next(NULL) {
}
ListNode() :val(0), next(NULL) {}
};
struct TreeNode {
int val;
TreeNode* pleft;
TreeNode* pright;
TreeNode(int a) :val(a), pleft(NULL), pright(NULL) {};
void PrintPre(TreeNode* pRoot) {
if (pRoot == NULL) return;
cout << pRoot->val << endl;
PrintPre(pRoot->pleft);
PrintPre(pRoot->pright);
return;
}
void PrintIn(TreeNode* pRoot) {
if (pRoot == NULL) return;
PrintIn(pRoot->pleft);
cout << pRoot->val << endl;
PrintIn(pRoot->pright);
return;
}
void PrintPost(TreeNode* pRoot) {
if (pRoot == NULL) return;
PrintPost(pRoot->pleft);
PrintPost(pRoot->pright);
cout << pRoot->val << endl;
return;
}
};
class Solution {
public:
bool Find(int target, vector<vector<int>> array) {
auto iVec = --array.end();
auto iLastEle = --iVec->end();
auto iVecbeg = array.begin();
auto iFirstEle = iVecbeg->begin();
if (target > * iLastEle) return false;
if (target < *iFirstEle) return false;
for (auto iVec = array.begin(); iVec != array.end(); ++iVec) {
if (target > * (--iVec->end())) continue;
for (auto iEle = iVec->begin(); iEle != iVec->end(); ++iEle) {
if (target == *iEle) return true;
}
}
return false;
};
void replaceSpace(char* str, int length) {
int ich = 0;
while (ich < length) {
if (str[ich] == ' ') {
int lenPG = (length - 1) - (ich + 1) + 1;
char* tempch = new char[lenPG];
char* tempch1 = tempch;
for (int i = ich + 1; i < length; ++i) {
tempch[i - ich - 1] = str[i];
}
str[ich++] = '%';
str[ich++] = '2';
str[ich++] = '0';
int ibe = ich;
char* tempch2 = tempch;
for (int i = 0; i < lenPG; ++i) {
str[ibe++] = tempch[i];
}
length = length + 2;
delete[]tempch;
continue;
}
else if (str[ich] == '\0') {
break;
}
else {
ich++;
continue;
}
}
str[ich] = '\0';
return;
/*char* newstr = new char[256];
int ich = 0;
int inew = 0;
while (ich<length) {
if (str[ich] != ' ') {
newstr[inew++] = str[ich];
++ich;
continue;
}else {
newstr[inew++] = '%';
newstr[inew++] = '2';
newstr[inew++] = '0';
++ich;
continue;
}
}
newstr[inew] = '/0';
str = newstr;*/
};
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> vectemp;
vectemp.clear();
if (head == NULL) return vectemp;
vectemp.push_back(head->val);
struct ListNode* pnext = head->next;
while (pnext != NULL) {
vectemp.push_back(pnext->val);
pnext = pnext->next;
}
vector<int> vecres;
vecres.clear();
auto ite = --vectemp.cend();
for (; ite != vectemp.cbegin(); --ite) {
vecres.push_back(*ite);
}
vecres.push_back(*ite);
return vecres;
};
TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin) {
vector<int>::iterator iRootVal = pre.begin();
TreeNode* Root = new TreeNode(*iRootVal);
if (pre.size() == 1) return Root;
vector<int> vinL;
vinL.clear();
vector<int>::iterator ipre = vin.begin();
for (; *ipre != *iRootVal; ++ipre) {
vinL.push_back(*ipre);
}
vector<int>vinR;
vinR.clear();
++ipre;
for (; ipre != vin.end(); ++ipre) {
vinR.push_back(*ipre);
}
vector<int>preL, preR;
preL.clear();
preR.clear();
for (auto i = (++pre.begin()); i != pre.end(); ++i) {
for (auto j = vinL.begin(); j != vinL.end(); ++j) {
if (*i == *j) {
preL.push_back(*i);
break;
}
}
for (auto k = vinR.begin(); k != vinR.end(); ++k) {
if (*i == *k) {
preR.push_back(*i);
break;
}
}
}
TreeNode* NodeL = new TreeNode(0);
if (preL.size() <= 0) {
NodeL = NULL;
}
else {
NodeL = reConstructBinaryTree(preL, vinL);
}
TreeNode* NodeR = new TreeNode(0);
if (preR.size() <= 0) {
NodeR = NULL;
}
else {
NodeR = reConstructBinaryTree(preR, vinR);
}
Root->pleft = NodeL;
Root->pright = NodeR;
return Root;
}
};
int main()
{
Solution mySol;
vector<int> vec1 = {1,2,3};
vector<int> vec2 = { 4,5,6 };
vector<int> vec3 = { 7,8,9 };
vector<vector<int>> vec = { vec1,vec2,vec3 };
bool bFound = mySol.Find(5,vec);
std::cout << "Hello World!\n";
std::cout << bFound << std::endl;
char str[256] = "We Are Happy";
cout << str << endl;
int len = strlen(str);
mySol.replaceSpace(str, len);
cout << str << endl;
int nCount = 0;
ListNode* head = new ListNode(0);
nCount++;
ListNode* pnode = head;
for (int i = 1; i < 3; ++i) {
ListNode* node = new ListNode(i);
pnode->next = node;
nCount++;
pnode = node;
}
ListNode** addressarr = new ListNode*[nCount];
ListNode** addresstemp = addressarr;
*addressarr = head;
ListNode* pnext = head->next;
while (pnext != NULL) {
addressarr++;
*addressarr = pnext;
pnext = pnext->next;
}
for (int i = 0; i < nCount; ++i) {
delete* addressarr;
addressarr--;
}
vector<int> pre = {1,2,4,7,3,5,6,8};
vector<int> vin = {4,7,2,1,5,3,8,6};
TreeNode* pRoot = mySol.reConstructBinaryTree(pre, vin);
pRoot->PrintPre( pRoot);
cout << endl;
pRoot->PrintIn(pRoot);
system("pause");
}