题解 | #牛群的编码反转#
牛群的编码反转
https://www.nowcoder.com/practice/fbbef1b8d84b45a49f95ebf63a3b353b
知识点:位运算
思路:遍历给定的数字,然后逐个与,最后存储结果即可
编程语言:java
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @return int整型
*/
public int reverseBits (int n) {
int result = 0;
for (int i = 0; i < 32; i++) {
//存储结果
result = (result << 1) + (n & 1);
n = n >> 1;
}
return result;
}
}
