注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
有点像贪心,当时没有做出来,在 get random number 那里卡了,后来就没时间了。下来用chatgpt整理下,希望对大家有帮助(求米看面筋🙏)
/**
Given:
A list of characters (e.g. ["a", "b", "c", "d"])
A constraint k (minimum gap between repeats of the same character)
Goal:
Randomly generate a sequence of characters such that no character repeats within k positions.
Valid example (k = 2)
a, b, c, a, d ✅add(card);
lastUsedIndex.put(card, currentIndex);
return card;
}
}
}
private String getRandomCard() {
int nextIndex = (int) (Math.random() * carList.size());
return carList.get(nextIndex);
}
} |