面试题29
题解链接
不用算法,直接模拟。
class Solution {
public:
vector<int> printMatrix(vector<vector<int> > matrix) {
vector<int> ans;
int l=0, r=matrix[0].size()-1, t=0, b=matrix.size()-1;
while(true){
for(int i=l; i<=r; ++i){
ans.push_back(matrix[t][i]);
}//向右
if(++t > b) break;
for(int i=t; i<=b; ++i){
ans.push_back(matrix[i][r]);
}
if(--r < l) break;
//向下
for(int i=r; i>=l; --i){
ans.push_back(matrix[b][i]);
}
if(--b < t) break;
//向左
for(int i=b; i>=t; --i){
ans.push_back(matrix[i][l]);
}
if(++l > r) break;
//向上
}
return ans;
}
};
面试题30
class Solution {
public:
stack<int> s1; //用于栈的push 与 pop
stack<int> s2; //用于存储最小min
void push(int value) {
s1.push(value);
if(s2.empty() || s2.top()>value){
s2.push(value);
}
else{
s2.push(s2.top());//这里,辅助栈要push一下,下面的pop是两个一起pop的
}
}
void pop() {
s1.pop();
s2.pop();
}
int top() {
return s1.top();
}
int min() {
return s2.top();
}
};
面试题31
题解链接
入栈n次,出栈n次,其实时间复杂度是O(n)
class Solution {
public:
bool IsPopOrder(vector<int> pushV,vector<int> popV) {
//模拟栈
stack<int> v;
int n=pushV.size();
int j=0;
for(int i=0; i<n ;++i){
v.push(pushV[i]);
while(!v.empty() && v.top()==popV[j] ){
//出栈
v.pop();
j++;
}
}
if(!v.empty()) return false;
return true;
}
};
面试题32
层序遍历,绝对不能出错
题解链接
class Solution {
public:
vector<int> PrintFromTopToBottom(TreeNode* root) {
//队列就行了
vector<int> v;
queue<TreeNode*> q;
if(!root) return v;
else q.push(root);
while(!q.empty()){
TreeNode* node=q.front();
v.push_back(node->val);
q.pop();
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
return v;
}
};
面试题33
分治法递归,时间O(n^2),空间O(n)
class Solution {
public:
bool helper(vector<int> v, int left, int right){
if(left>=right) return true;
int n=v.size();
int rootVal=v[right];//注意这里最后一个就是根,不用right-1
int mid=left;
while(mid<right){
if(v[mid]<rootVal){
mid++;
}
else break;
}//mid是右子树的开头
while(mid<right){
if(v[mid]>rootVal){
mid++;
}
else
return false;
}
return helper(v, left, mid-1) && helper(v, mid, right-1);
}
bool VerifySquenceOfBST(vector<int> sequence) {
int n=sequence.size();
if(n==0) return false;
return helper(sequence, 0, n-1);
}
};
迭代感觉不是很好看,暂时略过。
面试题34
题解链接
时间空间应该都是O(N)
class Solution {
public:
void dfs(TreeNode* root, int expectNumber, vector<vector<int>> &ans, vector<int> &path){
if(root==nullptr) return;
expectNumber=expectNumber-root->val;
path.push_back(root->val);
if(expectNumber==0){
if(root->left==nullptr && root->right==nullptr){
ans.push_back(path);
}
}
dfs(root->left, expectNumber, ans, path);
dfs(root->right, expectNumber, ans, path);
path.pop_back();
//注意这个pop
}
vector<vector<int>> FindPath(TreeNode* root,int expectNumber) {
//递归,分成子问题,但怎么记忆前面节点值
vector<vector<int>> ans;
vector<int> path;
dfs(root, expectNumber, ans, path);
return ans;
}
};
这题的BFS太长,意义不大,下次一定
面试题35
哈希表,空间O(n)
class Solution {
public:
unordered_map<RandomListNode*, RandomListNode*> cachedNode;
RandomListNode* Clone(RandomListNode* pHead) {
if(pHead == nullptr) return nullptr;
if(!cachedNode.count(pHead)){
RandomListNode* newHead=new RandomListNode(pHead->label);
cachedNode[pHead]=newHead;
newHead->next=Clone(pHead->next);
newHead->random=Clone(pHead->random);
//cachedNode[pHead]=newHead;
}
return cachedNode[pHead];
}
};
复制节点、遍历,
题解链接
class Solution {
public:
RandomListNode* Clone(RandomListNode* pHead) {
if(pHead==nullptr) return nullptr;
RandomListNode* cur=pHead;
//复制
while(cur!=nullptr){
RandomListNode* tmp=new RandomListNode(cur->label);
tmp->next=cur->next;
cur->next=tmp;
cur=tmp->next;
}
//设置random
cur=pHead;
while(cur!=nullptr){
if(cur->random!=nullptr)
cur->next->random=cur->random->next;
cur=cur->next->next;
}
//拆分两链表
cur=pHead->next;//遍历
RandomListNode* pre=pHead, *res=pHead->next;//旧链表头,新链表头
while(cur->next!=nullptr){
pre->next=pre->next->next;
cur->next=cur->next->next;
pre=pre->next;
cur=cur->next;
}
pre->next=nullptr;
cur->next=nullptr;
return res;
}
};
面试题36
题解链接
这题牛客和LC不一样,以下是牛客版本的解答。
class Solution {
public:
TreeNode* pre, *head;
void dfs(TreeNode* cur){
if(cur==nullptr) return;
dfs(cur->left);
if(pre != nullptr) pre->right=cur;
else head=cur;
cur->left=pre;
pre=cur;
dfs(cur->right);
}
TreeNode* Convert(TreeNode* pRootOfTree) {
//递归中序遍历就可,加上指针
if(pRootOfTree==nullptr) return nullptr;
dfs(pRootOfTree);
return head;
}
};