我无法弄清楚两个类如何与python相互交互。这是我的代码。
class Interval(object):
def __init__(self, s=0, e=0):
self.start = s
self.end = e
class Solution(object):
def canAttendMeetings(self, intervals):
"""
:type intervals: List[Interval]
:rtype: bool
"""
intervals.sort()
for i in range(1, len(intervals)):
if intervals[i].start < intervals[i-1].end:
return False
return True
B= Solution()
print B.canAttendMeetings([[20,30],[25,30]])
结果是'list'对象没有属性'start' 那么你能告诉我如何使用这两个类。
答案 0 :(得分:2)
您的代码执行此操作:
This should just be 2^14 as the offset has 14 bits.
传递
B.canAttendMeetings([[20,30],[25,30]])
在&#34;间隔&#34;参数:
[[25,30],[25,30]]
你和我知道他们应该是def canAttendMeetings(self, intervals):
但是你还没有告诉Python,所以据他所知,他们是列表。第一个是包含[25,30]的列表。
尝试以下方法:
Interval
打印:class Interval(object):
def __init__(self, s=0, e=0):
self.start = s
self.end = e
class Solution(object):
def canAttendMeetings(self, intervals):
"""
:type intervals: List[Interval]
:rtype: bool
"""
print type(intervals)
return True
B = Solution()
print B.canAttendMeetings([Interval(20,30),Interval(25,30)])
再次:间隔只是此时的列表,仅仅因为它被称为<type 'list'>
并不意味着它包含的内容。
为了使列表元素成为intervals
,您必须创建一些Interval
。
Interval
完整代码:
B.canAttendMeetings([Interval(20,30), Interval(25,30)])