Codility经典算法题之一 :binary gap

Codility(code+ablity)是一个主要面向面试者的OJ (online judge)平台,(Codility OJ is a platform for preparing technical coding interviews)。

目前coldility网站一共有63道经典算法题,网址:https://app.codility.com/programmers/lessons 

本博将会把题目以及我自己写的(这类会标注为原创),实在不会的是粘贴的其他版主的(这类会标注为转载)python版本的答案记录下来,与其说是答案,其实是一种思路,而且几乎不满足时间和空间复杂度,并且最终的测试也达不到100%(好难过),仅供python算法爱好者参考,如果大牛有好点子,请不吝赐教。

Task description:

binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps.

Write a function:

class Solution { public int solution(int N); }

that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.

For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5.

Assume that:

  • N is an integer within the range [1..2,147,483,647].

Complexity:

  • expected worst-case time complexity is O(log(N));
  • expected worst-case space complexity is O(1).
Solution:

def solution(N):

    cnt = 0
    result = 0
    found_one = False
    i = N
    while i:
        print(i)
        if i & 1 == 1:
            if (found_one == False):
                found_one = True
            else:
                result = max(result,cnt)
            cnt = 0
        else:
            cnt += 1
        i >>= 1
    return result
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`std::ios::binary` 是 C++ 中用于打开文件的打模式之一。 在打开文件时,可以通过指定 `std::ios::binary` 打开模式来确保以二进制模式读取或写入文件。这种模式对于处理二进制文件(例如图像、音频或视频)非常有用,因为它可以确保文件以二进制格式进行读写,而不会对数据进行任何额外的转换或处理。 以下是一些使用 `std::ios::binary` 的示例: ```cpp #include <iostream> #include <fstream> int main() { // 以二进制模式写入数据到文件 std::ofstream outputFile("data.bin", std::ios::binary); if (outputFile) { int data[] = { 1, 2, 3, 4, 5 }; outputFile.write(reinterpret_cast<const char*>(data), sizeof(data)); outputFile.close(); std::cout << "Data written to file." << std::endl; } else { std::cout << "Failed to open file for writing." << std::endl; } // 以二进制模式读取文件中的数据 std::ifstream inputFile("data.bin", std::ios::binary); if (inputFile) { int data[5]; inputFile.read(reinterpret_cast<char*>(data), sizeof(data)); inputFile.close(); std::cout << "Data read from file: "; for (int i = 0; i < 5; i++) { std::cout << data[i] << " "; } std::cout << std::endl; } else { std::cout << "Failed to open file for reading." << std::endl; } return 0; } ``` 在上面的示例中,我们首先以二进制模式将一些整数数据写入到名为 "data.bin" 的文件中,然后再以二进制模式从文件中读取数据并显示在控制台上。 通过指定 `std::ios::binary` 打开模式,我们可以确保数据以二进制形式进行读写,而不会进行任何额外的转换或处理。 希望这能帮助您理解 `std::ios::binary` 在C++中的使用。如有任何疑问,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值