LeetCode---2021/8/17

两两交换链表中的节点

在这里插入图片描述
分析:
  每次首先找到需要交换的两个结点,如果剩下的结点数<2则返回。假设要交换的两个结点是p1和p2,且满足temp->p1->p2,则交换操作为:

temp->next = p2;
p1->next = p2->next;
p2->next = p1;

代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* ptr = new ListNode(0);
        ptr->next = head;
        ListNode* temp = ptr;
        while(true) {
            if(temp->next && temp->next->next) {
                ListNode* p1 = temp->next;
                ListNode* p2 = temp->next->next;
                //交换
                temp->next = p2;
                p1->next = p2->next;
                p2->next = p1;
                temp = temp->next->next;
            }else {
                break;
            }
        }
        return ptr->next;
    }
};

学生出勤记录 I

在这里插入图片描述
分析:
  简单判断即可。
代码:

class Solution {
public:
    bool checkRecord(string s) {
        int n = s.size();
        int cnt = 0;
        for(int i = 0; i < n; i++) {
            if(s[i] == 'A') {
                cnt++;
            }
            if(i - 1 >= 0 && i + 1 < n) {
                if(s[i - 1] == 'L' && s[i] == 'L' && s[i + 1] == 'L') {
                    return false;
                }
            }
        }
        return cnt < 2;
    }
};

检查两个字符串数组是否相等

在这里插入图片描述
分析:
  简单判断即可。
代码:

class Solution {
public:
    bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
        string x = "", y = "";
        for(string t : word1) {
            x += t;
        }
        for(string t : word2) {
            y += t;
        }
        return x == y;
    }
};

K 个一组翻转链表

在这里插入图片描述
分析:
  首先找到需要翻转的k个结点的头结点与尾结点的下一个结点,然后逆序即可。
代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head, ListNode* s, int k) {
        ListNode* prev = s;  //
        ListNode* curr = head;
        for(int i = 0; i < k; i++) {
            ListNode* next = curr->next;
            curr->next = prev;
            prev = curr;
            curr = next;
        }
        return prev;  //翻转链表
    }
    
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode* ptr = new ListNode(0, head);
        ListNode* temp = ptr;
        while(true) {
            //先判断是否跳出
            ListNode *p = temp;
            int flag = 0;
            for(int i = 0; i < k; i++) {
                p = p->next;
                if(p == nullptr) {
                    flag = 1;
                    break;
                }
            }
            if(flag) {
                break;  //后面不足k个结点
            }
            temp->next = reverseList(temp->next, p->next, k);
            for(int i = 0; i < k; i++) {
                temp = temp->next;
            }
        }
        return ptr->next;
    }
};

N叉树的前序遍历

在这里插入图片描述
方法一:
  递归实现。
代码:

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<int> res;
    
    void dfs(Node* root) {
        if(root) {
            res.push_back(root->val);
            for(auto x : root->children) {
                dfs(x);
            }
        }
    }
    
    vector<int> preorder(Node* root) {
        dfs(root);
        return res;
    }
};

方法二:
  非递归实现。
代码:

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<int> res;
    vector<int> preorder(Node* root) {
        if(root == NULL) {
            return res;
        }
        stack<Node*> stk;
        stk.push(root);
        while(!stk.empty()) {
            Node* temp = stk.top();
            stk.pop();
            res.push_back(temp->val);
            reverse(temp->children.begin(), temp->children.end());
            for(auto x : temp->children) {
                stk.push(x);
            }
        }
        return res;
    }
};

N叉树的后序遍历

在这里插入图片描述
方法一:
  递归实现。
代码:

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<int> res;
    
    void dfs(Node* root) {
        if(root) {
            for(auto x : root->children) {
                dfs(x);
            }
            res.push_back(root->val);
        }
    }
    
    vector<int> postorder(Node* root) {
        dfs(root);
        return res;
    }
};

方法二:
  非递归实现。
代码:

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<int> res;
    vector<int> postorder(Node* root) {
        if(root == NULL) {
            return res;
        }
        stack<Node*> stk;
        stk.push(root);
        while(!stk.empty()) {
            Node* temp = stk.top();
            stk.pop();
            res.push_back(temp->val);
            // reverse(temp->children.begin(), temp->children.end());
            for(auto x : temp->children) {
                stk.push(x);
            }
        }
        reverse(res.begin(), res.end());  //逆序
        return res;
    }
};
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cyril_KI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值