本文主要是介绍今日头条编程题 - 珠子颜色【窗口滑动】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import java.util.ArrayList;
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);while (in.hasNextLine()) {int n = in.nextInt(); // 手串珠子数量int m = in.nextInt();// 连续m个。int c = in.nextInt(); // 颜色有很多种。ArrayList<ArrayList<Integer>> inputData = new ArrayList<>();for (int i = 0; i < n; i++) {int currentC = in.nextInt();ArrayList<Integer> oneData = new ArrayList<>();for (int j = 0; j < currentC; j++) {oneData.add(in.nextInt());}inputData.add(oneData);}getResult(n, m, c, inputData);}}public static void getResult(int n, int m, int c, ArrayList<ArrayList<Integer>> colors) {boolean[] result = new boolean[c + 1];int[] memo = new int[c + 1];//窗口的当前状态for (int i = 0; i < n + m; i++) {if (i < m) {putMemo(colors.get(i), memo);} else {outMemo(colors.get(i - m), memo);//移除窗口putMemo(colors.get(i % n), memo);//移入窗口}for (int k = 1; k <= c; k++) {if (memo[k] > 1) {result[k] = true;}}}int num = 0;for (int k = 1; k <= c; k++) {if (result[k]) {num++;}}System.out.println(num);}public static void putMemo(ArrayList<Integer> color, int[] memo) {for (Integer col : color) {memo[col]++;}}public static void outMemo(ArrayList<Integer> color, int[] memo) {for (Integer col : color) {memo[col]--;}}
}
这篇关于今日头条编程题 - 珠子颜色【窗口滑动】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!