博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 1238 Circular Permutation in Binary Representation 格雷码 gray code
阅读量:4186 次
发布时间:2019-05-26

本文共 919 字,大约阅读时间需要 3 分钟。

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 1:

Input: n = 2, start = 3Output: [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 = 2Output: [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

 

-------------------------------------------------------

格雷码,需要知道两点:

1. 格雷码对应卡诺图,所以可以循环:

2. 格雷码的计算可以通过i^(i>>1),其实i=range(n),代码为

class Solution:    def circularPermutation(self, n, start):        return [start ^ i ^ (i>>1) for i in range(1<

 

转载地址:http://utfoi.baihongyu.com/

你可能感兴趣的文章
哈希函数的逆向算法
查看>>
1-3 beanstalkd参数
查看>>
1-4 beanstalkd生产类
查看>>
1-5 beanstalkd消费类
查看>>
1-6 综合案例-生产者消费者
查看>>
织梦cms模板保护技术
查看>>
laravel 课程学习系列二----------------第二章.PHP框架安装之Laravel
查看>>
laravel 课程学习系列三----------------第三章.Artisan控制台
查看>>
git版本控制管理系列-----第四章 GIT基本概念
查看>>
mysql 库级权限、表级权限授权
查看>>
TensorFlow中的单层神经网络
查看>>
在TensorFlow中编程
查看>>
用c实现一个压力测试工具
查看>>
圆周率计算公式
查看>>
排序算法之-选择排序
查看>>
排序算法之-基数排序
查看>>
scikit-learn
查看>>
原生java方法操作SQLite数据库
查看>>
sqlite 数据库驱动框架
查看>>
B树、B+树、B*树 总结
查看>>