Giter Club home page Giter Club logo

interview-code-practice-python's Issues

剑指offer中倒数第三题出现的问题:程序可以运行,但在牛客网上报错 'NoneType' object has no attribute 'next' 不知道为什么??

def MeetingNode(self, pHead):
    if not pHead:
        return None
    pSlow = pHead.next #定义一个慢指针2
    pFast = pSlow.next#定义一个快指针3
    while pSlow != pFast : #判断快慢指针是否相等
            pSlow = pSlow.next
            pFast=pFast.next.next
            if pFast==None:
                break
    return pSlow,pFast

当链表中没有环时,pFast.next.next最后会变成None,但是代码中没有判断pFast.next.next是否为空的条件,因此我在代码中加入了判断条件
if pFast==None:
break

变态青蛙跳问题.py

如果是等比数列求和,解法是2^n - 1
return 2**n-1 ,与正确答案不符合。
所以f(n)=f(n-1)+f(n-2)+...+f(1)+f(0)
因为f(n-1)=f(n-2)+f(n-3)+...+f(1)+f(0)
f(0)=1,不是太懂,望指教。

快速排序小错误

# 快速排序

# 时间复杂度 O(nlogn) 空间复杂度 O(logn)-O(n)

x = [int(i) for i in input().split(',')]

def kpsort(x, first, last):

font = first

end = last

middle = x[first]

应改为 middle = x[last]

判断一个链表是否有环,如果有的话返回第一个进环的节点.py

应该是:if fast == None or slow == None: 才返回None吧?
而且是返回return fast.next这个才是第一个进环的结点,直接返回fast是前一个结点

我写了测试发现的,楼主看下
3 <main.Node object at 0x000001E414C26080>
4 <main.Node object at 0x000001E414C260B8>
5 <main.Node object at 0x000001E414C26048>
3 <main.Node object at 0x000001E414C26080>
4 <main.Node object at 0x000001E414C260B8>
5 <main.Node object at 0x000001E414C26048>
3 <main.Node object at 0x000001E414C26080>
4 <main.Node object at 0x000001E414C260B8>

结果= <main.Node object at 0x000001E414C26080>

小问题请教

第27行,为什么result不能用append添加链表的值?必须用extend吗?

'''
题目:输入一个链表,从尾到头打印链表每个节点的值
'''

'''
方法一:使用extend,在尾部插入,其实最关键在于[::-1],只不过输入数据多样化,有可能还是集合,所以转成列表
这个方法效率应该还可以,先存入vector,再反转vector
26ms
5512k
'''

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        if not listNode:
            return []

        result = []
        while listNode.next is not None:
            result.extend([listNode.val])
            listNode = listNode.next
        result.extend([listNode.val])

        return result[::-1]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.