美文网首页
LeetCode #1238 Circular Permutat

LeetCode #1238 Circular Permutat

作者: air_melt | 来源:发表于2022-08-07 12:31 被阅读0次

1238 Circular Permutation in Binary Representation 循环码排列

Description:

Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :

p[0] = start
p[i] and p[i+1] differ by only one bit in their binary representation.
p[0] and p[2^n -1] must also differ by only one bit in their binary representation.

Example:

Example 1:

Input: n = 2, start = 3
Output: [3,2,0,1]
Explanation: The binary representation of the permutation is (11,10,00,01).
All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2]

Example 2:

Input: n = 3, start = 2
Output: [2,6,7,5,4,0,1,3]
Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).

Constraints:

1 <= n <= 16
0 <= start < 2 ^ n

题目描述:

给你两个整数 n 和 start。你的任务是返回任意 (0,1,2,,...,2^n-1) 的排列 p,并且满足:

p[0] = start
p[i] 和 p[i+1] 的二进制表示形式只有一位不同
p[0] 和 p[2^n -1] 的二进制表示形式也只有一位不同

示例:

示例 1:

输入:n = 2, start = 3
输出:[3,2,0,1]
解释:这个排列的二进制表示是 (11,10,00,01)
所有的相邻元素都有一位是不同的,另一个有效的排列是 [3,1,0,2]

示例 2:

输出:n = 3, start = 2
输出:[2,6,7,5,4,0,1,3]
解释:这个排列的二进制表示是 (010,110,111,101,100,000,001,011)

提示:

1 <= n <= 16
0 <= start < 2^n

思路:

格雷码
参考LeetCode #89 Gray Code 格雷编码 - 简书 (jianshu.com)
核心公式: start ^ i ^ (i >> 1)
时间复杂度为 O(2 ^ n), 空间复杂度为 O(1)

代码:

C++:

class Solution 
{
public:
    vector<int> circularPermutation(int n, int start) 
    {
        vector<int> result(1 << n);
        for (int i = 0; i < (1 << n); i++) result[i] = (start ^ i ^ (i >> 1));
        return result;
    }
};

Java:

class Solution {
    public List<Integer> circularPermutation(int n, int start) {
        List<Integer> result = new ArrayList<>(1 << n);
        for (int i = 0; i < (1 << n); i++) result.add(start ^ i ^ (i >> 1));
        return result;
    }
}

Python:

class Solution:
    def circularPermutation(self, n: int, start: int) -> List[int]:
        return [start ^ i ^ (i >> 1) for i in range(2 ** n)]

相关文章

网友评论

      本文标题:LeetCode #1238 Circular Permutat

      本文链接:https://www.haomeiwen.com/subject/rwubwrtx.html