Leetcode day9

Leetcode day9,第1张

54. 螺旋矩阵【中等】

https://leetcode-cn.com/problems/spiral-matrix/

class Solution {
public:
    vector spiralOrder(vector>& matrix) {
        vector ans;
        if(matrix.size()==0||matrix[0].size()==0)
            return ans;
        int left = 0; 
        int right = matrix[0].size()-1;
        int up = 0;
        int down = matrix.size()-1;
        while(1)
        {
            if(left>right)
                break;
            for(int i=left; i<=right; i++)
                ans.push_back(matrix[up][i]);
            up++;

            if(downright)
                break;
            for(int i=right; i>=left; i--)
                ans.push_back(matrix[down][i]);
            down--;

            if(down=up; i--)
                ans.push_back(matrix[i][left]);
            left++;
        }
        return ans;
    }
};
199. 二叉树的右视图【中等】

https://leetcode-cn.com/problems/binary-tree-right-side-view/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector rightSideView(TreeNode* root) {
        vectorans;
        queueq;
        if(root==nullptr)
            return ans;
        q.push(root);
        int cnt=1;
        while(!q.empty())
        {
            int cnt1=cnt;
            cnt=0;
            while(cnt1--)
            {
                TreeNode*tmp = q.front();
                q.pop();
                
                if(tmp->left!=nullptr)
                {
                    q.push(tmp->left);
                    cnt++;
                }
                if(tmp->right!=nullptr)
                {
                    q.push(tmp->right);
                    cnt++;
                }
                if(cnt1==0)
                    ans.push_back(tmp->val);
            }
        }
        return ans;
    }
};
143. 重排链表【中等】

https://leetcode-cn.com/problems/reorder-list/

class Solution {
public:
   ListNode* reverselist(ListNode* head)
   {
       if(head==nullptr||head->next==nullptr)
           return head;
       ListNode* last = reverselist(head->next);
       head->next->next = head;
       head->next = nullptr;
       return last;
   }
   void reorderList(ListNode* head) {
       
       if(head==nullptr||head->next==nullptr)
           return;
       ListNode* cur = head;
       int cnt=0;
       while(cur!=nullptr)
       {
           cnt++;
           cur=cur->next;
       }
       int cnt1 = cnt/2;
       cur = head;
       int i=0;
       while(i++next;
       }

       ListNode* back = reverselist(cur);
       int tmp=0;
       ListNode* ptr1 = back;
       ListNode* ptr2 = head->next;
       ListNode* now = head;
       int cnt2 = cnt1-1;
       while(ptr1!=nullptr||cnt2)
       {
           if(ptr1==nullptr)
           {
               ptr2->next = nullptr;
               now->next = ptr2;
               break;
           }
           else if(cnt2==0)
           {
               now->next = ptr1;
               break;
           }
           else if(tmp%2==0)
           {
               now->next = ptr1;
               ptr1 = ptr1->next;
           }
           else if(tmp%2==1)
           {
               now->next = ptr2;
               ptr2 = ptr2->next;
               cnt2--;
           }
           now = now->next;
           tmp++;
       }

   }
};

704. 二分查找【简单】

https://leetcode-cn.com/problems/binary-search/

class Solution {
public:
    int search(vector& nums, int target) {
        int l=0;
        int r=nums.size();
        while(l=target)
                r = m;
            else l = m+1;
        }
        if(l==nums.size())
            return -1;
        return nums[l]==target ? l:-1;
    }
};

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/673623.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-19
下一篇 2022-04-19

发表评论

登录后才能评论

评论列表(0条)

保存