力扣,https://leetcode.cn/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof/description/
思路:https://leetcode.cn/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof/solutions/215895/mian-shi-ti-56-ii-shu-zu-zhong-shu-zi-chu-xian-d-4/
有限状态自动机推导式太复杂,略去~
class Solution {
public int trainingPlan(int[] actions) {
int res = 0, m = 3;
int[] counter = new int[32]; // java中int型数据包含4个byte, 1byte=8bit, 共32位;最高位是符号位
for (int act : actions) {
for (int i = 0; i < 32; ++i) {
counter[i] += act & 1;
act >>= 1;
}
}
for (int i = 31; i >= 0; --i) {
res <<= 1;
res += counter[i] % m; // 这俩顺序不能颠倒, 否则会导致最后往左多挪动1位
}
return res;
}
}