LeetCode-中等-29. 乐团站位

LeetCode-中等-29. 乐团站位

题目:

引用自:LeetCode-中等-29. 乐团站位(如有侵权联系删除)

某乐团的演出场地可视作 num * num 的二维矩阵 grid(左上角坐标为 [0,0]),每个位置站有一位成员。乐团共有 9 种乐器,乐器编号为 1~9,每位成员持有 1 个乐器。

为保证声乐混合效果,成员站位规则为:自 grid 左上角开始顺时针螺旋形向内循环以 1,2,...,9 循环重复排列。例如当 num = 5 时,站位如图所示:
在这里插入图片描述

请返回位于场地坐标 [Xpos,Ypos] 的成员所持乐器编号。

示例 1:

输入:num = 3, Xpos = 0, Ypos = 2

输出:3

解释:在这里插入图片描述

示例 2:

输入:num = 4, Xpos = 1, Ypos = 2

输出:5

解释:在这里插入图片描述

提示:

  • 1 <= num <= 10^9
  • 0 <= Xpos, Ypos < num

解题:

没什么好说的,主要分两步:

1: 找到这个位位置在第几圈上

2: 找到该位置在该圈上的第几个坐标,用外圈的所有位置数,加上处于该圈的第几个位置之后,可得到从左上角开始一个个数,螺旋数到当前,一共数了几个数,最后用这个数模9就ok啦。

代码:

class Solution {
    public int orchestraLayout(int num, int xPos, int yPos) {
        int xCircleNum = num - 1 - xPos;
        // 想象一下 找他在第几圈的时候,该位置是关于矩阵中心点对称的 比如5*5的矩阵,第0行和第4行都是在第一圈上
        xCircleNum = Math.min(xCircleNum, xPos);
        int yCircleNum = num - 1 - yPos;
        yCircleNum = Math.min(yCircleNum, yPos);
        long circleNum = Math.min(xCircleNum, yCircleNum);

        Long numberInOwnerCircle;

        if (xPos <= yPos)
            // 如果该位置在该圈的右上部分
            numberInOwnerCircle = (xPos + yPos - circleNum * 2 + 1);
        else{
            numberInOwnerCircle = ((num - 1 - circleNum * 2) * 2 + 1);
            numberInOwnerCircle += ((num - 1 - circleNum) * 2 - xPos - yPos);
        }

        long res = numberInOwnerCircle;

        if( circleNum > 0){
            res += (num - 1 + (num - 1 - (circleNum - 1) * 2)) * circleNum * 2;
        }

        return (int)((res % 9 == 0) ? 9 : res % 9);
    }
}

在这里插入图片描述

测试:

class Solution {
    public int orchestraLayout(int num, int xPos, int yPos) {
        int xCircleNum = num - 1 - xPos;
        // 想象一下 找他在第几圈的时候,该位置是关于矩阵中心点对称的 比如5*5的矩阵,第0行和第4行都是在第一圈上
        xCircleNum = Math.min(xCircleNum, xPos);
        int yCircleNum = num - 1 - yPos;
        yCircleNum = Math.min(yCircleNum, yPos);
        long circleNum = Math.min(xCircleNum, yCircleNum);

        Long numberInOwnerCircle;

        if (xPos <= yPos)
            // 如果该位置在该圈的右上部分
            numberInOwnerCircle = (xPos + yPos - circleNum * 2 + 1);
        else{
            numberInOwnerCircle = ((num - 1 - circleNum * 2) * 2 + 1);
            numberInOwnerCircle += ((num - 1 - circleNum) * 2 - xPos - yPos);
        }

        long res = numberInOwnerCircle;

        if( circleNum > 0){
            res += (num - 1 + (num - 1 - (circleNum - 1) * 2)) * circleNum * 2;
        }

        return (int)((res % 9 == 0) ? 9 : res % 9);
    }

    public static void main(String[] args) {
        Solution solution1 = new Solution1();
        System.out.println("solution1.orchestraLayout(5,2,2) = " + solution1.orchestraLayout(5, 2, 2));
        System.out.println("solution1.orchestraLayout(449572,209397,306801) = " + solution1.orchestraLayout(449572, 209397, 306801));
    }
}

在这里插入图片描述

总结:

好一段时间没咋刷题了,手生,不能放下呀,放下生的是真快!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值