java answer


  • 0
    3

    public List<Integer> grayCode(int n) {
    if(n==0){
    List<Integer> list=new ArrayList<>();
    list.add(0);
    return list;
    }
    List<Integer> list=grayCode(n-1);
    for(int i=list.size()-1;i>=0;i--){
    int temp=list.get(i)+(int)Math.pow(2,n-1);
    list.add(temp);
    }
    return list;
    }


Log in to reply
 

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