码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode4.26

时间:2020-04-27 15:24:23      阅读:47      评论:0      收藏:0      [点我收藏+]

标签:bool   mirror   快速排序   find   sts   first   tno   旋转   val   

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

标签:bool   mirror   快速排序   find   sts   first   tno   旋转   val   

原文地址:https://www.cnblogs.com/wfplingyun/p/12779003.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!