leeguandong / interview-code-practice-python Goto Github PK
View Code? Open in Web Editor NEW面试题
面试题
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
如果是等比数列求和,解法是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,不是太懂,望指教。
应改为 middle = x[last]
并未改变链表顺序
应该是: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>
visited| {(row, col)} 这是什么语法
Interview-code-practice-python/机试题/排序.py
Line 50 in 86933f8
第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]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.