久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Python:在整數(shù)的二進(jìn)制表示中查找最長(zhǎng)的二進(jìn)制間

Python: Find longest binary gap in binary representation of an integer number(Python:在整數(shù)的二進(jìn)制表示中查找最長(zhǎng)的二進(jìn)制間隙)
本文介紹了Python:在整數(shù)的二進(jìn)制表示中查找最長(zhǎng)的二進(jìn)制間隙的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問題描述

我想知道我的實(shí)施是否有效.我試圖使用 python 找到該問題的最簡(jiǎn)單和低復(fù)雜度的解決方案.

I would like to know if my implementation is efficient. I have tried to find the simplest and low complex solution to that problem using python.

def count_gap(x):
    """
        Perform Find the longest sequence of zeros between ones "gap" in binary representation of an integer

        Parameters
        ----------
        x : int
            input integer value

        Returns
        ----------
        max_gap : int
            the maximum gap length

    """
    try:
        # Convert int to binary
        b = "{0:b}".format(x)
        # Iterate from right to lift 
        # Start detecting gaps after fist "one"
        for i,j in enumerate(b[::-1]):
            if int(j) == 1:
                max_gap = max([len(i) for i in b[::-1][i:].split('1') if i])
                break
    except ValueError:
        print("Oops! no gap found")
        max_gap = 0
    return max_gap

讓我知道你的意見.

推薦答案

您的實(shí)現(xiàn)將整數(shù)轉(zhuǎn)換為基數(shù)為 2 的字符串,然后訪問字符串中的每個(gè)字符.相反,您可以使用 <<& 訪問整數(shù)中的每一位.這樣做將避免訪問每個(gè)位兩次(首先將其轉(zhuǎn)換為字符串,然后檢查結(jié)果字符串中是否為1").它還將避免為字符串分配內(nèi)存,然后為您檢查的每個(gè)子字符串分配內(nèi)存.

Your implementation converts the integer to a base two string then visits each character in the string. Instead, you could just visit each bit in the integer using << and &. Doing so will avoid visiting each bit twice (first to convert it to a string, then to check if if it's a "1" or not in the resulting string). It will also avoid allocating memory for the string and then for each substring you inspect.

您可以通過(guò)訪問 1 << 來(lái)檢查整數(shù)的每一位.0, 1 <<1, ..., 1 <<(x.bit_length).

You can inspect each bit of the integer by visiting 1 << 0, 1 << 1, ..., 1 << (x.bit_length).

例如:

def max_gap(x):
    max_gap_length = 0
    current_gap_length = 0
    for i in range(x.bit_length()):
        if x & (1 << i):
            # Set, any gap is over.
            if current_gap_length > max_gap_length:
                max_gap_length = current_gap_length
            current_gap_length = 0
         else:
            # Not set, the gap widens.
            current_gap_length += 1
    # Gap might end at the end.
    if current_gap_length > max_gap_length:
        max_gap_length = current_gap_length
    return max_gap_length

這篇關(guān)于Python:在整數(shù)的二進(jìn)制表示中查找最長(zhǎng)的二進(jìn)制間隙的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 在线看亚洲 | 成人av一区二区亚洲精 | 成人午夜视频在线观看 | 日韩av在线播 | 成人在线观看免费 | 中文字幕在线视频免费观看 | 日韩成人av在线 | 99精品视频免费观看 | 亚洲色图50p| 99爱在线观看| 日韩一二区在线 | 日本高清aⅴ毛片免费 | 国产丝袜一区二区三区免费视频 | 国产成人网 | 久久久久一区二区 | 九九热精品视频在线观看 | 亚洲成人久久久 | 日韩视频成人 | 91精品国产91久久久久久 | 精品一区二区三区在线视频 | 久久电影一区 | 中文字幕免费视频 | 人人人人干 | 免费看片在线播放 | 国产精品久久国产精品 | 99久久精品免费看国产高清 | 午夜av一区二区 | 欧美激情一区二区三区 | 福利视频日韩 | 中文字幕一区在线 | 91精品麻豆日日躁夜夜躁 | 国产一区二区三区四区五区3d | 亚洲免费久久久 | 免费一看一级毛片 | 日韩欧美高清dvd碟片 | 成人三级av | 久久香蕉网 | 久久精品成人 | 欧美日韩精品在线一区 | 国产精品日韩高清伦字幕搜索 | 这里只有精品99re |