89. Gray Code
Last updated
Last updated
/**
* Time complexity :
* Space complexity :
*/
class Solution {
public List<Integer> grayCode(int n) {
int noOfGrays = (1 << n);
return IntStream.range(0, noOfGrays)
.mapToObj(i -> (i ^ (i>>1)))
.collect(Collectors.toList());
}
}/**
* Time complexity :
* Space complexity :
*/
class Solution {
public List<Integer> grayCode(int n) {
// Number of possible gray codes (i.e, 2^n)
int noOfGrays = (1 << n);
List<Integer> grayCodes = new ArrayList<>();
for(int i = 0; i < noOfGrays; i++) {
grayCodes.add(i ^ (i>>1));
}
return grayCodes;
}
}