Datawhale-leetcode第7天打卡(leetcode保姆级教程)——题号054,059,061

Datawhale-leetcode第7天打卡(leetcode保姆级教程)——题号054,059,061

第一题54. 螺旋矩阵

题目描述:

给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

示例 1:

输入:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
输出: [1,2,3,6,9,8,7,4,5]
示例 2:

输入:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]

完整代码

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        int a[4][2]={0,1,1,0,0,-1,-1,0};
        vector<int> res;
        vector<vector<int>> aa(matrix.size()+2,vector<int>(matrix[0].size()+2,0));
        int index1=0;
        int index2=0;
        int fangxing=0;

        while(res.size()!=matrix.size()*matrix[0].size())
        {
            if(aa[index1+1][index2+1]==0 && index1<matrix.size() && index2<matrix[0].size() && index1>-1&&index2>-1)
            {
                res.push_back(matrix[index1][index2]);
                aa[index1+1][index2+1]=1;
                index1=index1+a[fangxing][0];
                index2=index2+a[fangxing][1];
            }
            else{
                index1=index1-a[fangxing][0];
                index2=index2-a[fangxing][1];
                fangxing=(fangxing+1)%4;
                index1=index1+a[fangxing][0];
                index2=index2+a[fangxing][1];
            }
        }
        return res;

    }
};

记得在leetcode里运行。

第二题59. 螺旋矩阵 II

给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。

示例:

输入: 3
输出:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]

代码如下

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        int a[4][2]={0,1,1,0,0,-1,-1,0};
        vector<vector<int>> res(n,vector<int>(n,0));
        vector<vector<int>> aa(n+2,vector<int>(n+2,0));
        int index1=0;
        int index2=0;
        int fangxing=0;
        int flag=0;
        
        vector<int> shuju;
        for(int i=1;i<n*n+1;i++)
        {
            shuju.push_back(i);
        }
        

        while(flag<shuju.size())
        {
            if(aa[index1+1][index2+1]==0 && index1<n && index2<n && index1>-1&&index2>-1)
            {
                res[index1][index2]=shuju[flag];
                flag++;
                aa[index1+1][index2+1]=1;
                index1=index1+a[fangxing][0];
                index2=index2+a[fangxing][1];
            }
            else{
                index1=index1-a[fangxing][0];
                index2=index2-a[fangxing][1];
                fangxing=(fangxing+1)%4;
                index1=index1+a[fangxing][0];
                index2=index2+a[fangxing][1];
            }
        }
        return res;

    }
};

第三题61. 旋转链表

这个o(n)就可以解决,分治看不懂。用一个最小值每次更新迭代,到小于0就重新更新为0

题目描述:

给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。

示例 1:

输入: 1->2->3->4->5->NULL, k = 2
输出: 4->5->1->2->3->NULL
解释:
向右旋转 1 步: 5->1->2->3->4->NULL
向右旋转 2 步: 4->5->1->2->3->NULL
示例 2:

输入: 0->1->2->NULL, k = 4
输出: 2->0->1->NULL
解释:
向右旋转 1 步: 2->0->1->NULL
向右旋转 2 步: 1->2->0->NULL
向右旋转 3 步: 0->1->2->NULL
向右旋转 4 步: 2->0->1->NULL

直接上代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        ListNode *pst = head;
        ListNode *last = NULL;
        int count = 0;
        while(pst != NULL){
            ++count;
            last = pst;
            pst = pst->next; 
        }
        if(count == 0){
            return head;
        }
        int actual = k % count;
        last -> next = head;
        pst = head;
        for(int i = 0;i < count - actual - 1;++i){
            pst = pst->next;
        }
        head = pst->next;
        return head;
    }
};

解决任务!(想念世界上最可爱的CC)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值