题解 | #向左移动牛群II#

向左移动牛群II

https://www.nowcoder.com/practice/2467ddd80a2942abbaa752f3c874dd79

#include <cmath>
#include <vector>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @param k int整型 
     * @return int整型vector<vector<>>
     */
    vector<vector<int> > rotateII(int n, int k) {
        // write code here
        // 模拟
        vector<vector<int>> ans; 
    
        for(int i=1; i<=n; ++i)
        {
            vector<int> temp;
            int t = 1;
            for(int j=1; j<=n; ++j)
                temp.emplace_back((i-1)*n+j);
            ans.emplace_back(temp);
        }

        // 可以将向做和向上分开执行
        // 执行向左移动
        for(int i=0; i<k; ++i)
        {
            for(int r=0; r<n; ++r)
            {
                int temp = ans[r][0];
                for(int c=0; c+1<n; ++c)
                    ans[r][c] = ans[r][c+1];
                ans[r][n-1] = temp;
            }
        }
        
        // 执行向上移动
        for(int i=0; i<k; ++i)
        {
            for(int c=0; c<n; ++c)
            {
                int temp = ans[0][c];
                for(int r=0; r+1<n; ++r)
                    ans[r][c] = ans[r+1][c];
                ans[n-1][c] = temp;
            }
        }

        return ans;
    }
};

虚数五行区解题中心 文章被收录于专栏

非淡泊无以明志,非宁静无以致远

全部评论

相关推荐

白火同学:先说结论,准大三不是特别好找实习,boss沟通300+没有实习是很正常的情况。一是暑期实习时间太短了,二是在这么多准大四都找不到实习,从实习时间和掌握技术层面,企业会优先看他们。 再说简历,其实985本+准大三到这水平的简历也很优秀了,要说的话,项目经历可以再优化一下,可以基本围绕采取STAR原则,分为项目概述、技术架构、技术亮点、实现结果,再发给AI润色一下。 最后说操作,准大三的话,如果想找实习那就多投,不过现在也7月中旬了,时间上已经略晚了。如果7月底实在找不到,也可以多刷点算法,多学点技术,这实习也不至于一定得有,当然有更好。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务