61、序列化二叉树
https://blog.youkuaiyun.com/malele4th/article/details/79335409
https://blog.youkuaiyun.com/u013575812/article/details/50185719
class Solution {
vector<int> vec;
public:
void dfs1(TreeNode *p)
{
if (!p) vec.push_back(0x23333);
else
{
vec.push_back(p->val);
dfs1(p->left);
dfs1(p->right);
}
}
TreeNode* dfs2(int* &p)
{
if (*p == 0x23333)
{
++p;
return nullptr;
}
TreeNode* res = new TreeNode(*p);
++p;
res->left = dfs2(p);
res->right = dfs2(p);
return res;
}
char* Serialize(TreeNode *root) {
vec.clear();
dfs1(root);
int *res = new int[vec.size()];
for (int i = 0; i < vec.size(); i++) res[i] = vec[i];
return (char*)res;
}
TreeNode* Deserialize(char *str) {
int *p = (int*)str;
return dfs2(p);
}
};
62、二叉搜索树的第k个结点
https://blog.youkuaiyun.com/sun10081/article/details/82805991
https://blog.youkuaiyun.com/lzuacm/article/details/51328529
class Solution {
public:
TreeNode* KthNode(TreeNode* pRoot, unsigned int k)
{
TreeNode *p=pRoot;
TreeNode *resNode;
if(p==NULL || k==0) return NULL;
stack<TreeNode *> st;
unsigned int count=0;
while(!st.empty() || p != NULL)
{
if(p != NULL)
{
st.push(p);
p=p->left;
}
if(p == NULL)
{
p=st.top(); // 取当前节点
count++;
if(count==k) resNode=p;
st.pop();
p=p->right;
}
}
return resNode;
}
};
63、数据流中的中位数
https://blog.youkuaiyun.com/derrantcm/article/details/46872339
https://blog.youkuaiyun.com/malele4th/article/details/79335602
class Solution {
public:
priority_queue<int, vector<int>, less<int>>p;
priority_queue < int, vector<int>, greater<int>>q;
void Insert(int num)
{
if (p.empty() || num < p.top())
p.push(num);
else
q.push(num);
if (p.size() == q.size() + 2)
{
q.push(p.top());
p.pop();
}
if (p.size() + 1 == q.size())
{
p.push(q.top());
q.pop();
}
}
double GetMedian()
{
return p.size() > q.size() ? p.top() : (q.top()+p.top())/2.0;
}
};
63、滑动窗口的最大值:
https://blog.youkuaiyun.com/malele4th/article/details/79336991
https://blog.youkuaiyun.com/derrantcm/article/details/46872411
class Solution {
public:
vector<int> maxInWindows(const vector<int>& num, unsigned int size)
{
vector<int>res;
deque<int>q;
for (int i = 0; i < num.size(); i++)
{
while (q.size() && num[q.back()] <= num[i]) q.pop_back();
while (q.size() && i - q.front() + 1 > size)q.pop_front();
q.push_back(i);
if (size&&i + 1 >= size)
res.push_back(num[q.front()]);
}
return res;
}
};
64、矩阵中的路径
https://blog.youkuaiyun.com/derrantcm/article/details/46887767
https://blog.youkuaiyun.com/malele4th/article/details/79337003
class Solution {
public:
bool hasPath(char* matrix, int rows, int cols, char* str)
{
vector<bool> used(strlen(matrix), false);
if (str == nullptr)return true;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (has(matrix, rows, cols,i,j,str,0, used))
return true;
}
}
return false;
}
bool has(char*matrix, int rows, int cols,int row,int col, char* str, int length, vector<bool>used)
{
if (strlen(str) == length) return true;
if (row > rows || row<0 || col>cols || col < 0) return false;
int index = row * cols + col;
bool result = false;
if (!used[index] && matrix[index] == str[length])
{
used[index] = true;
result = has(matrix, rows, cols, row+1,col, str, length + 1, used) ||
has(matrix, rows, cols, row-1,col, str, length + 1, used) ||
has(matrix, rows, cols, row, col+1, str, length + 1, used) ||
has(matrix, rows, cols, row, col-1, str, length + 1, used);
}
if (result) return true;
return false;
}
};
65、机器人的运动范围
https://blog.youkuaiyun.com/malele4th/article/details/79337046
https://blog.youkuaiyun.com/derrantcm/article/details/46887811
class Solution {
public:
int movingCount(int threshold, int rows, int cols)
{
int **flag = new int*[rows];
for (int i = 0; i < rows; i++)
{
flag[i] = new int[cols];
}
return count(0,0,rows,cols,flag,threshold);
}
int count(int i, int j, int rows, int cols, int **flag,int threshold)
{
if (i < 0 || j<0 || j >= cols || i >= rows || sum(i) + sum(j)>threshold || flag[i][j] == 1) return 0;
flag[i][j] = 1;
return count(i + 1, j, rows, cols, flag, threshold)
+ count(i - 1, j, rows, cols, flag, threshold)
+ count(i, j + 1, rows, cols, flag, threshold)
+ count(i, j - 1, rows, cols, flag, threshold)+1;
}
int sum(int x)
{
int sum = 0;
do { sum += x % 10; }
while ((x=x/10)>0);
return sum;
}
};
完结