劍指offer每日六題---------day four
劍指offer題19:輸入一個矩陣,按照從外向裡以順時針的順序依次打印出每一個數字,eg. 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
則依次打印出1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
#include<vector> vector<int> PrintMaterix(vector<vector<int>> matrix) { vector<int> v; int up = 0, down = matrix.size(); int left = 0, right = matrix[0].size(); while (up < down && left < right)//順時針列印 { int i = up, j = left - 1; while (++j < right) v.push_back(matrix[i][j]);//上橫 --j; if (up + 1 == down){ ++up; continue; } while (++i < down) v.push_back(matrix[i][j]);//右豎 --i; if (right - 1 == left){ --right; continue; } while (--j >= left) v.push_back(matrix[i][j]);//下橫 ++j; while (--i > up) v.push_back(matrix[i][j]);//左豎 ++i; up++; right--; down--; left++; } return v; }
劍指offer題20:定義一個棧,使它實現一個min函式,min函式能再O(1)取出棧中最小的資料
#include<stack> class Stack { public: void Push(int x) { s.push(x); if (help.empty()) help.push(x); else { if (x < help.top()) help.push(x); else help.push(help.top()); } } void pop() { s.pop(); help.pop(); } int Min() { return help.top(); } private: stack<int> s; stack<int> help; };
劍指offer題21:輸入兩個整數序列,第一個序列表示棧的壓入順序,請判斷第二個序列是否為該棧的彈出順序
#include<vector>
#include<stack>
bool IsPopOrder(vector<int> pushV, vector<int> popV)
{
if (pushV.size() != popV.size())return false;
stack<int> sta;
size_t pop_index = 0;
for (size_t i = 0; i < pushV.size(); ++i)
{
if (pushV[i] != popV[pop_index])
sta.push(pushV[i]);
else
++pop_index;
}
while (!sta.empty() && sta.top() == popV[pop_index++])
sta.pop();
return sta.empty() ? true : false;
}
劍指offer題22:層序遍歷一棵二叉樹
#include<queue>
vector<int> PrintFromTopToBottom(TreeNode* root)
{
if (!root)return vector<int>();
vector<int> dst;
queue<TreeNode*> q;
q.push(root);
while (!q.empty())
{
TreeNode *front = q.front();
dst.push_back(front->val);
q.pop();
if (front->left)q.push(front->left);
if (front->right)q.push(front->right);
}
return dst;
}
劍指offer題23:輸入一個整數陣列,判斷該陣列是不是某二叉搜尋樹的後序遍歷的結果。(陣列中任意兩個數都不相同)
方法一:遞迴,對於二叉搜尋樹的後序遍歷結果,去掉該陣列中的最後一個數據(也就是根),那麼陣列分成兩部分,
前部分是左子樹,後部分是右子樹
方法二:非遞迴,基於遞迴思想寫成非遞迴,O(n^2)
//方法一:
bool Judge(vector<int>& post, int left, int right)
{
if (left >= right)return true;
bool flag = false;
int li = left, ri = right - 1;
while (li < right && post[li] < post[right])++li;
while (ri >= left && post[ri] > post[right])--ri;
if (li > ri)flag = true;
return flag
&& Judge(post, left, ri)
&& Judge(post, li, right - 1);
}
bool VerifySquenceOfBST(vector<int> post)
{
if (post.empty())return false;
return Judge(post, 0, post.size() - 1);
}
方法二:
bool VerifySquenceOfBST2(vector<int> post)
{
int size = post.size();
while (--size)
{
int count = size, index = size - 1;
while (index >= 0 && post[index] > post[size]){ --count; --index; }
while (index >= 0 && post[index] < post[size]){ --count; --index; }
if (count != 0)return false;
}
return true;
}
劍指offer題24:輸入一棵二叉樹和一個整數,打印出二叉樹中結點值的和為輸入整數的所有路徑。
路徑定義為數的根結點開始往下一直到葉節點所經過的結點形成一條路徑
void _FindPath(TreeNode *root, vector<int>&res,vector<vector<int>> &dst,int sum, int target)
{
if (!root)return;
vector<int> res_tmp = res;
res_tmp.push_back(root->val);
if ((sum += root->val) == target && !root->left && !root->right)
dst.push_back(res_tmp);
if (sum <= target)
_FindPath(root->left, res_tmp, dst, sum, target);
if (sum <= target)
_FindPath(root->right, res_tmp, dst, sum, target);
}
vector<vector<int>> FindPath(TreeNode* root, int target)
{
vector<int> res;
vector<vector<int>>dst;
int sum = 0;
_FindPath(root, res, dst, sum, target);
return dst;
}