Class Solution

java.lang.Object
g0001_0100.s0089_gray_code.Solution

public class Solution extends Object
89 - Gray Code.<p>Medium</p> <p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</strong> in the sequence,</li> <li>The binary representation of every pair of <strong>adjacent</strong> integers differs by <strong>exactly one bit</strong> , and</li> <li>The binary representation of the <strong>first</strong> and <strong>last</strong> integers differs by <strong>exactly one bit</strong>.</li> </ul> <p>Given an integer <code>n</code>, return <em>any valid <strong>n-bit gray code sequence</strong></em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 2</p> <p><strong>Output:</strong> [0,1,3,2]</p> <p><strong>Explanation:</strong> The binary representation of [0,1,3,2] is [00,01,11,10]. - 00 and 01 differ by one bit - 01 and 11 differ by one bit - 11 and 10 differ by one bit - 10 and 00 differ by one bit [0,2,3,1] is also a valid gray code sequence, whose binary representation is [00,10,11,01]. - 00 and 10 differ by one bit - 10 and 11 differ by one bit - 11 and 01 differ by one bit - 01 and 00 differ by one bit</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 1</p> <p><strong>Output:</strong> [0,1]</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 16</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details