Nidhi 创造了另一个版本的地铁跑酷游戏[关闭]

问题描述 投票:0回答:0

自定义测试用例1: 1个 4个 0 1 2 3 0

执行

结果 你的输出 2

预期产出 2个 自定义测试用例 2: 1个 9 0 3 0 2 0 2 1 2 2 0

执行

结果 你的输出 4

预期产出 1个 自定义测试用例 3: 3个 1个 0 0 2个 0 2 0 3个 0 3 3 0

执行

结果 你的输出 0 1个 1

预期产出 0 1个 0 自定义测试用例 4: 2个 2个 0 0 0 9 0 1 1 1 1 3 3 3 2 0

执行

结果 你的输出 0 2

预期产出 0 1

import java.util.Scanner;

public class Solution {
    static public int minLaneChanges(int[] manholes) {
    int n = manholes.length - 1;
    int currLane = 1;
    int laneChanges = 0;
    for (int i = 1; i <= n; i++) {
        if (manholes[i] != currLane && manholes[i] != 0) {
            if (manholes[i] == manholes[i - 1]) {
                currLane = manholes[i];
            } else {
                currLane = manholes[i];
                laneChanges++;
            }
        }
    }
    return laneChanges;
}

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            int[] manholes = new int[n + 1];
            for (int i = 0; i <= n; i++) {
                manholes[i] = sc.nextInt();
            }
            System.out.println(minLaneChanges(manholes));
        }
    }
}
java algorithm grid
© www.soinside.com 2019 - 2024. All rights reserved.