51-55题

作者: yy辰 | 来源:发表于2018-10-16 12:45 被阅读14次

51、不用加减乘除做加法
直接用sum函数。看别人的题解用二进制的异或和与运算符。

# -*- coding:utf-8 -*-
class Solution:
    def Add(self, num1, num2):
        # write code here
        return sum([num1, num2])

52、二叉搜索树与双向链表
想了一会没想出来,虽然知道使用中序遍历加递归,但是用代码实现不了。于是参考别人的代码:

class Solution:

    def __init__(self):
        self.head = None
        self.tail = None

    def Convert(self, pRootOfTree):
        # write code here
        if not pRootOfTree:
            return None
        self.Convert(pRootOfTree.left)
        if self.head == None:
            self.head, self.tail = pRootOfTree, pRootOfTree
        else:
            self.tail.right = pRootOfTree
            pRootOfTree.left = self.tail
            self.tail = pRootOfTree
        self.Convert(pRootOfTree.right)
        return self.head

53、复杂链表的复制
没有想到好的办法,参考了别人的思路:https://blog.csdn.net/qq_33431368/article/details/79296360
可以说是很巧妙了。不过好像直接用字典写起来比较快。

# -*- coding:utf-8 -*-
class RandomListNode:
    def __init__(self, x):
        self.label = x
        self.next = None
        self.random = None

class Solution:
    # 返回 RandomListNode
    def Clone(self, pHead):
        # write code here
        if not pHead:
            return None
        cur = pHead
        while cur:
            node = RandomListNode(cur.label)
            node.next = cur.next
            cur.next = node
            cur = node.next

        cur = pHead
        while cur:
            node = cur.next
            if cur.random:
                node.random = cur.random.next
            cur = cur.next.next

        old = pHead
        pHead = pHead.next
        while old.next:
            node = old.next
            old.next = node.next
            old = node
        return pHead

53、字符串的排列
可以用递归写

class Solution:
    def Permutation(self, ss):
        # write code here
        def getall(ss):
            if not ss:
                return None
            if len(ss) == 1:
                return [ss]
            rst = []
            for i in range(len(ss)):
                rest = getall(ss[:i] + ss[i + 1:])
                for j in rest:
                    rst.append(ss[i] + j)
            return rst

        temp = getall(ss)
        return sorted(list(set(temp)))

自己测试的没问题,但是牛客网上过不了。不知道是不是因为python版本的问题,牛客网也不会像leetcode一样说哪个测试用例过不了。

55、二进制中1的个数
补码不知道是啥= =。计算机基础知识不扎实,先放着吧

相关文章

  • 51-55题

    51、不用加减乘除做加法直接用sum函数。看别人的题解用二进制的异或和与运算符。 52、二叉搜索树与双向链表想了一...

  • 51-55

    晓霞和少平,两个可爱的年轻人,因为灵魂的靠近,终于走在了一起,真替你们感到高兴。不管未来的路怎么样,这样美丽美妙的...

  • 诗篇51-55

    诗篇 第51篇 〔大卫与拔示巴同室以后,先知拿单来见他。他作这诗,交与伶长。〕 上帝啊,求你按你的慈爱怜恤我,按你...

  • 笔畅作文三四年级10.19名著共读课程预告

    内容:《红沙发音乐城》51-55 时间:2018.10.19晚8点 主讲:张萍 预习问题:1、爸爸有没有发现皮皮鲁...

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

    这篇文章是“100个基本”有感系列第十一篇,将记录51-55条“基本”的感悟。 051 模仿、学习、赞扬尊敬的人。...

  • 51-55讲总结

    051讲:组织承诺 | 四种共同体 [if !supportLists]一,[endif]本篇的本质是什么? Wh...

  • 少儿编程游戏CodeMonkey通关攻略:第51-55关

    本文介绍第51-55关。 第51关 猴博士说,我们用代码让河狸把木桩推到猴子和香蕉之间的地方。 beavers[1...

  • 2022-02-21

    分享285+22 今天读书(刘老师读书打卡) 《建构解决之道》许维素 51-55页 最近这几天怠慢了我家的猫主...

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

    ❤ 诵读《示弟立志说》&道德经51-55章 ❤ 阅读《有钱人和你想的不一样》 ❤️家业同修营加课《财富档案4》 ❤...

  • 以德报怨 何以报德 别在消耗自己了

    ྃ 读书思考|《人性的枷锁》51-55章 你有没有思考过人生的意义是什么 书中主人翁菲利普说是为了各尽其职 ...

网友评论

      本文标题:51-55题

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