-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy path089-GrayCode.cs
More file actions
33 lines (29 loc) · 818 Bytes
/
089-GrayCode.cs
File metadata and controls
33 lines (29 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//-----------------------------------------------------------------------------
// Runtime: 288ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _089_GrayCode
{
public IList<int> GrayCode(int n)
{
var result = new List<int>() { 0 };
if (n == 0) return result;
result.Add(1);
var pointer = 1;
while (pointer < n)
{
var value = 1 << pointer;
for (var i = result.Count - 1; i >= 0; i--)
{
result.Add(value + result[i]);
}
pointer++;
}
return result;
}
}
}