8 line elegant java code


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

Log in to reply
 

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