leetcode4.26
Posted wfplingyun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode4.26相关的知识,希望对你有一定的参考价值。
1、23合并K个排序链表:优先队列的方法
class Solution
{
public:
struct cmp
{
bool operator()(ListNode* a, ListNode* b)
{
return a->val > b->val;
}
};
ListNode* mergeKLists(vector<ListNode*>& lists)
{
priority_queue<ListNode*, vector<ListNode*>, cmp>q;
for (auto elem : lists)
{
if (elem)q.push(elem);
}
ListNode head (-1);
ListNode* p = &head;
while (!q.empty())
{
ListNode* top = q.top();
q.pop();
if (top->next)q.push(top->next);
p->next = top; p = top;
}
return head.next;
}
};
2、101对称二叉树————利用2叉数的广度遍历
//bfs
class Solution {
public:
bool isSymmetric(TreeNode* root) {
queue<TreeNode*>q;
q.push(root);
while (!q.empty())
{
vector<int>v = {};
int size = q.size();
while (size)
{
TreeNode* top = q.front();
if (top)
{
v.push_back(top->val);
}
else
{
v.push_back(INT_MIN);
}
q.pop();
if (top)
{
q.push(top->left);
q.push(top->right);
}
size--;
}
int i = 0, j = v.size() - 1;
while (i < j)
{
if (v[i] != v[j])return false;
i++;
j--;
}
}
return true;
}
};
//递归
class Solution
{
public:
bool isMirror(TreeNode* t1, TreeNode* t2)
{
if (!t1 && !t2)return true;
if (!t1 || !t2)return false;
return ((t1->val == t2->val) && isMirror(t1->left, t2->right) &&
isMirror(t1->right, t2->left));
}
bool isSymmetric(TreeNode* root)
{
if (!root)return true;
else return isMirror(root->left, root->right);
}
};
3、102二叉树的层次遍历
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if (!root)return{};
vector<vector<int>>result;
queue<TreeNode*>q;
q.push(root);
while (!q.empty())
{
vector<int>v = {};
int size = q.size();
while (size)
{
TreeNode* top = q.front();
v.push_back(top->val);
q.pop();
if (top->left)q.push(top->left);
if (top->right)q.push(top->right);
size--;
}
result.push_back(v);
}
return result;
}
};
4、103旋转层次遍历
class Solution
{
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root)
{
queue<TreeNode*>Queue;
vector<vector<int>>res;
if (!root)return {};
TreeNode* p = root;
Queue.push(p);
int cout = 0;
while(!Queue.empty())
{
vector<int>temp;
int len = Queue.size();
for (int i = 0; i < len; i++)
{
p = Queue.front();
if (cout % 2 == 0)
{
temp.push_back(p->val);
}
else
{
temp.insert(temp.begin(), p->val);
}
Queue.pop();
if (p->left)Queue.push(p->left);
if (p->right)Queue.push(p->right);
}
cout++;
res.push_back(temp);
}
return res;
}
};
5、104二叉树的最大深度
class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root)return 0;
else
{
int d1 = maxDepth(root->left) + 1;
int d2 = maxDepth(root->right) + 1;
return (d1 > d2 ? d1 : d2);
}
}
};
6、136只出现一次的数字————考察异或
```cpp
//哈希表
class Solution {
public:
int singleNumber(vector<int>& nums)
{
unordered_map<int, int>m;
int len = nums.size();
for (int i = 0; i < len; i++)
{
if (m.find(nums[i]) == m.end())
{
m[nums[i]] = 1;
}
else
{
m.erase(nums[i]);
}
}
return m.begin()->first;
}
};
//异或
class Solution
{
public:
????????int singleNumber(vector<int>& nums)
????????{
???????????????int v = 0;
???????????????int len = nums.size();
???????????????for (int i = 0; i < len; i++)
???????????????{
???????????????????????v = v ^ nums.at(i);
???????????????}
???????????????return v;
????????}
};
7、快速排序
```cpp
class Solution
{
public:
void quicksort(vector<int>& nums, int left, int right)
{
int i = left;
int j = right;
if (i>= j)return;//递归必须有出口呀
int temp = nums[left];
while (i < j)
{
while ((nums[j] > temp) && (i < j))
{
j--;
}
swap(nums[i], nums[j]);
while ((nums[i] <= temp) && (i < j))
{
i++;
}
swap(nums[i], nums[j]);
}
quicksort(nums, left, i - 1);
quicksort(nums, i + 1, right);
}
};
8、141环形链表
//哈希表
class Solution {
public:
bool hasCycle(ListNode* head) {
unordered_map<ListNode*, int>m;
ListNode* p = head;
while (p)
{
if (m.find(p) == m.end())
{
m[p] = 1;
p = p->next;
}
else return true;
}
return false;
}
};
以上是关于leetcode4.26的主要内容,如果未能解决你的问题,请参考以下文章