栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

leetcode 234题/264题/338题 一比特位 python3题解

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

leetcode 234题/264题/338题 一比特位 python3题解

234题 回文链表

方法一:快慢指针+反转链表

这个O1好难。设置反转链表的函数,再设置快慢指针来找到链表一半的节点(快指针永远比慢指针快一步,所以当快指针走到尽头慢指针正好在链表的一半处)。反转后面一半链表来检查前半部分链表==后半部分链表。

class Solution:
    def isPalindrome(self, head: ListNode) -> bool:
        first_half_end = self.end_of_first_half(head)
        second_half_start = self.reverseList(first_half_end.next)

        first_position = head
        second_position = second_half_start
        while second_position:
            if first_position.val != second_position.val:
                return False
            first_position = first_position.next
            second_position = second_position.next
        return True

    def reverseList(self, head: ListNode) -> ListNode:
        if not head or not head.next: return head
        cur = self.reverseList(head.next) 
        head.next.next = head
        head.next = None
        return cur

    def end_of_first_half(self, head):
        fast = slow = head
        while fast.next and fast.next.next:
            fast = fast.next.next
            slow = slow.next
        return slow

方法二:遍历链表

class Solution:
    def isPalindrome(self, head: ListNode) -> bool:
        vals, node = [], head
        while node:
            vals.append(node.val)
            node = node.next
        return vals == vals[::-1]

264题 丑数

#比如1,2,3现在3个丑数,下一个丑数4使用了2乘积那么下标+1,
#对于因子2来说:不再使用2作为基本乘数。
#1,2,3,4现在4个丑数,下一个丑数5使用了5乘积下标+1,
#对于因子5来说,不再使用1作为基本乘数。
#即开始考虑1后面的乘数作为基本乘数。这就是下标+1的意义,使用过该因子后,对于此因子来说将前面的乘数扔掉不再考虑

这个动态规划的思想非常好。。

class Solution:
    def nthUglyNumber(self, n: int) -> int:
        dp = [1] * n
        index5 = index2 = index3 = 0
        for i in range(1, n):
            dp[i] = min(dp[index2]*2, dp[index3]*3, dp[index5]*5)
            if dp[i] == 2 * dp[index2]: index2 += 1
            if dp[i] == 3 * dp[index3]: index3 += 1
            if dp[i] == 5 * dp[index5]: index5 += 1
        return dp[n-1]

338题 比特位计数

只能想到O(nlogn)时间复杂度的常规解答,遍历每个i,对每个i计数1的比特位。官答3个dp给我整懵了。。应该仔细研究一下。另外在常规解答里,如果设定ans数组来遍历时间会比写函数调用函数计算一比特位

DP 1:​​​​​​​最高有效位

对于0<=j

对于2的幂y,y只有最高位数为1,即y的一比特位是1.

对于0<=y

解释:因为y的1在最高位,x比y大的数值z,单拎出来的二进制表达式与x的后面几位是完全相同的,所以x的一比特位只比z的一比特位多最高位的那个1.

当遇到下一个2的幂,更新最高有效位数。

class Solution:
    def countBits(self, n: int) -> List[int]:
        ans = [0] * (n+1)
        for i in range(1, n+1):
            if (i & i-1) == 0:
                highbits = i
            ans[i] = ans[i-highbits] + 1
        return ans
DP 2:​​​​​​​最低有效位

官答起名叫最低有效位我觉得不太好理解,它的思路其实简单,不如起名降次法...

对于任意一个数x,右移一位,即抛弃最低位,那么bits{x} = bits{x/2} + a.

当抛弃的最低位是1,那么a=1,否则0.

x/2的操作是降低原先的幂次,因为二进制少一位即是降低1幂次。

class Solution:
    def countBits(self, n: int) -> List[int]:
        ans = [0] * (n+1)
        for i in range(n+1):
            ans[i] = ans[i>>1] + (i&1) #这里i>>1与i//2同效果 后者略慢
        return ans
#不提前声明数组空间而使用append也略慢
DP 3:​​​​​​​最低设置位

还是拿i&i-1做功夫,此操作会删除最后一个1.

即对于y来说,x=y&y-1, bits{y} = bits{x} + 1

class Solution:
    def countBits(self, n: int) -> List[int]:
        ans = [0] * (n+1)
        for i in range(1, n+1):
            ans[i] = ans[i&i-1] + 1
        return ans

位运算真的博大精深!!!!果然是计算机的最基础

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/283489.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号