89. Gray Code

https://leetcode.com/problems/gray-code/discuss/29893/One-liner-Python-solution-(with-demo-in-comments)

 

 

 

 1 class Solution {
 2     public List<Integer> grayCode(int n) {
 3         List<Integer> res = new ArrayList<>();
 4         res.add(0);
 5         for(int i = 0; i < n; i++) {
 6             for(int j = res.size() - 1; j >= 0 ; j--) {
 7                 res.add(res.get(j) + (int)Math.pow(2, i));
 8             }
 9         }
10         return res;
11     }
12 }

 

posted @ 2018-09-16 23:55  jasoncool1  阅读(93)  评论(0编辑  收藏  举报