[LeetCode] Gray Code

vector<int> grayCode(int n) {
	vector<int> gray_code;
	gray_code.push_back(0);
	if(n == 0)
		return gray_code;
	gray_code.push_back(1);

	int cnt = 2;
	while(cnt <= n)
	{
		vector<int> gray_code_tmp = gray_code;
		vector<int>::reverse_iterator iter;
		for(iter = gray_code_tmp.rbegin(); iter != gray_code_tmp.rend(); iter++)
		{
			int num = *iter + (1<<(cnt-1)); 
			gray_code.push_back(num);
		}
		cnt++;
	}
	return gray_code;         
}

你可能感兴趣的:([LeetCode] Gray Code)