3133.数组最后一个元素的最小值
链接:3133.数组最后一个元素的最小值
难度:Medium
标签:位运算
简介:返回 nums[n - 1] 可能的 最小 值。
题解 1 - python
- 编辑时间:2024-08-22
- 执行用时:31ms
- 内存消耗:16.3MB
- 编程语言:python
- 解法介绍:把两个字符串进行拼接,在保留原有x的二进制位的基础上增加n的遍历
class Solution:
    def minEnd(self, n: int, x: int) -> int:
        nstr = list(bin(n - 1)[2:])
        xstr = list(bin(x)[2:])
        nstr.reverse()
        xstr.reverse()
        res = []
        for bit in xstr:
            if bit == '0' and nstr: res.append(nstr.pop(0))
            else: res.append(bit)
        while nstr: res.append(nstr.pop(0))
        res.reverse()
        return eval('0b' + ''.join(res))