56-60题

作者: yy辰 | 来源:发表于2018-10-17 19:52 被阅读6次

56、链表中倒数第K节点
因为之前做了好几道双指针的题,所以联想到这道题也能用双指针。
但是不知道为什么不能AC

class Solution:
    def FindKthToTail(self, head, k):
        # write code here
        if not head:
            return None
        fast, slow = head, head
        for i in range(1, k):
            if not fast:
                return None
            fast = fast.next
        while fast.next:
            fast = fast.next
            slow = slow.next
        return slow

57、合并两个排序链表

class Solution(object):
    def mergeTwoLists(self, l1, l2):
        head = ListNode(-1)
        temp = head
        while l1 and l2:
            if l1.val <= l2.val:
                temp.next = l1
                l1 = l1.next
                temp = temp.next
            else:
                temp.next = l2
                l2 = l2.next
                temp = temp.next
        if l1:
            temp.next = l1
        if l2:
            temp.next = l2
        return head.next

58、翻转链表

class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:
            return None
        a, b, c = None, head, head.next
        while c:
            b.next = a
            a, b, c = b, c, c.next
        b.next = a
        return b

59、树的子结构

class Solution:
    def HasSubtree(self, pRoot1, pRoot2):
        # write code here
        if pRoot1 == None or pRoot2 == None:
            return False
        return self.isSubtree(pRoot1, pRoot2)

    def isSubtree(self, p1, p2):
        if p2 == None:
            return True
        if p1 == None:
            return p1 == p2
        res = False
        if p1.val == p2.val:
            res = self.isSubtree(p1.left, p2.left) and self.isSubtree(p1.right, p2.right)
        return res or self.isSubtree(p1.left, p2) or self.isSubtree(p1.right, p2)

60、数值的整数次方
看了别人的代码,利用右移一位运算代替除以2
利用位与运算代替了求余运算法%来判断一个数是奇数还是偶数

# -*- coding:utf-8 -*-
class Solution:
    def Power(self, base, exponent):
        # write code here
        if exponent == 0:
            return 1
        if exponent == 1:
            return base
        if exponent == -1:
            return 1/base

        ans = self.Power(base, exponent >> 1)
        ans = ans * ans
        if exponent & 1 == 1:
            ans = ans * base
        return ans

相关文章

  • 56-60题

    56、链表中倒数第K节点因为之前做了好几道双指针的题,所以联想到这道题也能用双指针。但是不知道为什么不能AC 57...

  • 56-60

    56 人类从房屋里进入大自然的时候,并不是像野兽脱离了囚笼,而是感觉到自己置身于上帝之中。 57 人尽可以鄙夷生活...

  • 诗篇56-60

    诗篇 第56篇 〔非利士人在迦特拿住大卫。那时,他作这金诗,交与伶长。调用“远方无声鸽”。〕 上帝啊,求你怜悯我!...

  • 第十六周总结

    一、学习内容 经典:学习《周易•下经》第56-60卦,复习第31-55卦。 诗词:《凉州词》王翰 地理:《中国地理...

  • 读《100个基本》有感-最基本的也是最有意义的(12)

    这篇文章是“100个基本”有感系列第十二篇,将记录56-60条“基本”的感悟。 056 认真听他人讲话,持续、深入...

  • 学习老子的智慧

    (补)✅ 11.12阅读《老子的心事》56-60页Day12 1.我读到了什么? 故从事于道者,同于道;德者,同于...

  • 2020幸福实修内外丰盛,家业同修14/100

    今日实修: ❤ 诵读《示弟立志说》&道德经56-60章 ❤ 阅读《有钱人和你想的不一样》 ❤️谐韵瑜伽练习 ❤️今...

  • 56-60讲小结

    056讲:信任短缺|团队协作的杀手 [if !supportLists]一,[endif]本篇的本质是什么? Wh...

  • 56-60讲总结

    what:团队冲突 1,团队冲突的定义:团队冲突指的是两个或两个以上的团队在目标、利益、认识等方面互不相容或互相排...

  • 少儿编程游戏CodeMonkey通关攻略:第56-60关

    下面进入正文,我在这篇文章里介绍第56-60关的通关攻略,这几关主要介绍for循环的用法。 在介绍关卡前,我先简要...

网友评论

      本文标题:56-60题

      本文链接:https://www.haomeiwen.com/subject/kodlzftx.html