Is this an acceptable solution ?


  • 0
    T

    I solved this problem using bits manipulation and got accepted. However I am just wondering, can we solve it like i mentioned below.
    Gray codes would always be numbers starting from 0 to (2^n - 1).

    public class Solution {
    public List<Integer> grayCode(int n) {
        List<Integer> listObj = new ArrayList<Integer>();
        for(int i=0;i<Math.pow(2,n);i++){
            listObj.add(i);
        }
        return listObj;
    }
    }
    

    Thanks.


  • 0
    F

    No, it's not. The order of the listobj is 0,1,2,3.....2^n-1; but the gray code is not.


  • 0
    T

    Ya. that's what i was thinking. thanks.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.