89. Gray Code #Medium

leetcode 89. Gray Code #Medium
The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
00 - 0
01 - 1
11 - 3
10 - 2
题意
根据所给数字n生成2^n个连续的格雷码
分析
举例
n=1, [0, 1]
n=2, [00, 01, 11, 10]
n=3, [000, 001, 011, 010, 110, 111, 101, 100]
后一个序列的前半段就是前一个序列,后半段是前一个序列的每个数字左边补1,然后反向排列这些数字。根据此规律可求解
C++代码

class Solution {
public:
    vector
  
  
   
    grayCode(int n) {
        vector
   
   
    
     r1(1,0),r2;
        if(n<1)return r1;
        r1.push_back(1);
        for(int i=1,j;i
    
    
     
     =0;j--)r2.push_back(r1[j]+(1<
     
     
    
    
   
   
  
  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是关于使用OpenCV进行人脸检测的基本步骤: 1. 导入OpenCV库 ```python import cv2 ``` 2. 读取图片 ```python img = cv2.imread('image.jpg') ``` 3. 加载人脸模型(级联分类器)特征人脸建模 ```python face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') ``` 4. 对图片进行灰度处理 ```python gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ``` 5. 检查人脸 ```python faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) ``` 6. 标记人脸 ```python for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) ``` 7. 显示图片 ```python cv2.imshow('img', img) ``` 8. 暂停窗口 ```python cv2.waitKey(0) ``` 9. 销毁窗口 ```python cv2.destroyAllWindows() ``` 完整代码如下: ```python import cv2 # 读取图片 img = cv2.imread('image.jpg') # 加载人脸模型(级联分类器)特征人脸建模 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # 对图片进行灰度处理 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 检查人脸 faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) # 标记人脸 for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) # 显示图片 cv2.imshow('img', img) # 暂停窗口 cv2.waitKey(0) # 销毁窗口 cv2.destroyAllWindows() ``` 注意:在运行这段代码之前,需要先下载并保存级联分类器文件(haarcascade_frontalface_default.xml)在与代码文件相同的目录下。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值