两两交换链表中的节点
分析:
每次首先找到需要交换的两个结点,如果剩下的结点数<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;
}
};