# LeetCode - 0089. Gray Code ### 題目網址:https://leetcode.com/problems/gray-code/ ###### tags: `LeetCode` `Medium` `數學` `位元運算` ```cpp= /* -LeetCode format- Problem: 89. Gray Code Difficulty: Medium by Inversionpeter */ vector <int> answer; class Solution { public: vector<int> grayCode(int n) { int graycode = 0, amount = 1 << n, index = 0; answer.resize(amount); while (index < amount) { answer[index] = graycode; ++index; graycode ^= 1; answer[index] = graycode; ++index; graycode ^= (graycode & (-graycode)) << 1; } return answer; } }; ```