Leetcode day9

54. 螺旋矩阵【中等】

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

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<int> 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(down<up)
                break;
            for(int i=up; i<=down; i++)
                ans.push_back(matrix[i][right]);
            right--;

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

            if(down<up)
                break;
            for(int i=down; i>=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<int> rightSideView(TreeNode* root) {
        vector<int>ans;
        queue<TreeNode*>q;
        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++<cnt1)
       {
           cur = cur->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<int>& nums, int target) {
        int l=0;
        int r=nums.size();
        while(l<r)
        {
            int m = l+(r-l)/2;
            if(nums[m]>=target)
                r = m;
            else l = m+1;
        }
        if(l==nums.size())
            return -1;
        return nums[l]==target ? l:-1;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值