return s.count("U") == s.count("D") and s.count("L") == s.count("R")为您提供了一个线性解决方案,可以通过以下方式将其优化为单个过程 from collections import Counterd = Counter(moves)return d["D"] == d["U"] and d["R"] == d["L"] 至于你的密码, for moves in sequences:...