题解 | #向左移动牛群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;
    }
};

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

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

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
asdasdasda...:19岁,不容易啊可能升个本会好点,现在学历歧视太严重了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 12:10
直接上图
牛客13578115...:改得一般,不值80
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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