剑指offer打卡 week3
35. 反转链表
https://www.acwing.com/problem/content/33/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* prev=NULL;
ListNode* cur=head;
while(cur){
auto next=cur->next;
cur->next=prev;
prev=cur;
cur=next;
}
return prev;
}
};
36. 合并两个排序的链表
https://www.acwing.com/problem/content/34/
class Solution {
public:
ListNode* merge(ListNode* l1, ListNode* l2) {
if(l1==NULL) return l2;
if(l2==NULL) return l1;
ListNode* head=new ListNode(-1);
auto p=head;
while(l1!=NULL && l2!=NULL){
if(l1->val<=l2->val){
p->next=l1;
p=p->next;
if(l1->next==NULL){
p->next=l2;
break;
}
l1=l1->next;
}
else {
p->next=l2;
p=p->next;
if(l2->next==NULL){
p->next=l1;
break;
}
l2=l2->next;
}
}
return head->next;
}
};
37. 树的子结构
https://www.acwing.com/problem/content/35/
class Solution {
public:
bool hasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
if(pRoot1==NULL || pRoot2==NULL) return false;
if(fun(pRoot1,pRoot2)) return true;
else return hasSubtree(pRoot1->left,pRoot2) || hasSubtree(pRoot1->right,pRoot2);
}
bool fun(TreeNode* p1,TreeNode*p2){
if(!p2) return true;
if(!p1 || p1->val!=p2->val) return false;
return fun(p1->left,p2->left) && fun(p1->right,p2->right);
}
};
38. 二叉树的镜像
https://www.acwing.com/problem/content/37/
class Solution {
public:
void mirror(TreeNode* root) {
if(!root) return ;
swap(root->left,root->right);
mirror(root->left);
mirror(root->right);
}
};
39. 对称的二叉树
https://www.acwing.com/problem/content/38/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
return !root || dfs(root->left, root->right);
}
bool dfs(TreeNode*p, TreeNode*q)
{
if (!p || !q) return !p && !q;
return p->val == q->val && dfs(p->left, q->right) && dfs(p->right, q->left);
}
};
40. 顺时针打印矩阵
https://www.acwing.com/problem/content/39/
class Solution {
public:
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int st[1005][1005]={0};
vector<int> printMatrix(vector<vector<int> > matrix) {
vector<int>ans;
int n=matrix.size();
if(n==0) return ans;
int m=matrix[0].size();
if(m==0) return ans;
int cnt=n*m;
int k=0;
int x=0,y=0;
ans.push_back(matrix[0][0]);
st[0][0]=1;
while(--cnt){
int nx=x+dx[k];
int ny=y+dy[k];
if(nx>=0 && nx<n && ny>=0 && ny<m && !st[nx][ny]){
ans.push_back(matrix[nx][ny]);
st[nx][ny]=1;
x=nx;
y=ny;
}
else {
k++;
k%=4;
nx=x+dx[k];
ny=y+dy[k];
ans.push_back(matrix[nx][ny]);
st[nx][ny]=1;
x=nx;
y=ny;
}
}
return ans;
}
};
41. 包含min函数的栈
https://www.acwing.com/problem/content/90/
class MinStack {
public:
/** initialize your data structure here. */
stack<int>stackValue;
stack<int>minstack;
MinStack() {
}
void push(int x) {
stackValue.push(x);
if(minstack.empty() || minstack.top()>=x){
minstack.push(x);
}
}
void pop() {
if (minstack.top() == stackValue.top()) minstack.pop();
stackValue.pop();
}
int top() {
return stackValue.top();
}
int getMin() {
return minstack.top();
}
};
42. 栈的压入、弹出序列
https://www.acwing.com/problem/content/40/
class Solution {
public:
stack<int>s1;
bool isPopOrder(vector<int> pushV,vector<int> popV) {
int n=pushV.size();
int m=popV.size();
if(n!=m){
return false;
}
if(n==0){
return true;
}
int j=0;
for(int i=0;i<n;i++){
//cout<<s1.top()<<endl;
s1.push(pushV[i]);
while(!s1.empty() && s1.top()==popV[j]){
j++;
s1.pop();
if(j==m-1){
return true;
}
}
}
return false;
}
};
43. 不分行从上往下打印二叉树
https://www.acwing.com/problem/content/41/
class Solution {
public:
vector<int> printFromTopToBottom(TreeNode* root) {
vector<int> res;
if (!root) return res;
queue<TreeNode*> q;
q.push(root);
while (q.size()) {
auto t = q.front();
q.pop();
res.push_back(t->val);
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
}
return res;
}
};
44. 分行从上往下打印二叉树
https://www.acwing.com/problem/content/42/
class Solution {
public:
typedef pair<TreeNode*,int> PII;
vector<vector<int>>ans;
vector<int>temp;
vector<vector<int>> printFromTopToBottom(TreeNode* root) {
if(!root) return ans;
queue<PII>q;
q.push({root,1});
int k=1;
while(q.size()){
auto q1=q.front();
auto first=q1.first;
auto second=q1.second;
q.pop();
if(second!=k){
ans.push_back(temp);
temp.clear();
k++;
}
temp.push_back(first->val);
if(first->left){
q.push({first->left,second+1});
}
if(first->right){
q.push({first->right,second+1});
}
}
if(temp.size()!=0){
ans.push_back(temp);
}
return ans;
}
};
45. 之字形打印二叉树
https://www.acwing.com/problem/content/43/
和上题一样就是了 直接用别人代码
class Solution {
public:
vector<vector<int>> printFromTopToBottom(TreeNode* root) {
vector<vector<int>> res;
if(!root) return res;
queue<TreeNode*> q;
q.push(root);
bool flag=true;
while(!q.empty()){
vector<int> tmp;
int size=q.size();
while(--size>=0){
auto n=q.front();
tmp.push_back(n->val);
q.pop();
if(n->left)q.push(n->left);
if(n->right)q.push(n->right);
}
if(!flag) reverse(tmp.begin(),tmp.end());
flag=!flag;
res.push_back(tmp);
}
return res;
}
};

浙公网安备 33010602011771号