码迷,mamicode.com
首页 > 其他好文 > 详细

Gray Code

时间:2015-05-22 07:03:59      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

public class Solution {
    public ArrayList<Integer> grayCode(int n) {
        if(n==0){
            ArrayList<Integer> res = new ArrayList<Integer>();
            res.add(0);
            return res;
        }
        ArrayList<Integer> res =  grayCode(n-1);
        int tmp = 1<< (n-1);
        int size = res.size(); // 注意这个size必须要计算,不能直接用n
        for(int i=size-1;i>=0;i--){
            res.add(res.get(i)+tmp);
        }
        return res;
    }
}

 

Gray Code

标签:

原文地址:http://www.cnblogs.com/jiajiaxingxing/p/4521299.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!