秋意正浓,我又开始怀念那个13了,于是找到了两道题号13开头的leetcode题,并ac了它们,突然觉得美滋滋的。
1396. 设计地铁系统
class UndergroundSystem
def initialize()
@h = {}
@hid = {}
end
=begin
:type id: Integer
:type station_name: String
:type t: Integer
:rtype: Void
=end
def check_in(id, station_name, t)
unless @hid.has_key?(id)
@hid[id] = [[station_name,t]]
else
@hid[id].push([station_name,t])
end
end
=begin
:type id: Integer
:type station_name: String
:type t: Integer
:rtype: Void
=end
def check_out(id, station_name, t)
@hid[id][-1].push(station_name)
@hid[id][-1].push(t)
unless @h.has_key?(@hid[id][-1][0]+"_"+@hid[id][-1][2])
@h[@hid[id][-1][0]+"_"+@hid[id][-1][2]] = []
@h[@hid[id][-1][0]+"_"+@hid[id][-1][2]].push(@hid[id][-1][3]-@hid[id][-1][1])
else
@h[@hid[id][-1][0]+"_"+@hid[id][-1][2]].push(@hid[id][-1][3]-@hid[id][-1][1])
end
end
=begin
:type start_station: String
:type end_station: String
:rtype: Float
=end
def get_average_time(start_station, end_station)
@h[start_station+"_"+end_station].sum/@h[start_station+"_"+end_station].length.to_f
end
end
# Your UndergroundSystem object will be instantiated and called as such:
# obj = UndergroundSystem.new()
# obj.check_in(id, station_name, t)
# obj.check_out(id, station_name, t)
# param_3 = obj.get_average_time(start_station, end_station)
class ProductOfNumbers
def initialize()
@num = []
@h = {}
end
=begin
:type num: Integer
:rtype: Void
=end
def add(num)
if @num.length == 0 && num > 0
@num.push(num)
elsif @num.length > 0 && num > 0 && @num[-1] != 0
@num.push(@num[-1]*num)
elsif @num.length == 0 && num == 0
@num.push(num)
@h[0] = 0
elsif @num.length > 0 && num == 0
@num.push(0)
@h[0] = @num.length - 1
elsif @num.length > 0 && num > 0 && @num[-1] == 0
@num.push(num)
end
end
=begin
:type k: Integer
:rtype: Integer
=end
def get_product(k)
if @num[-k] == 0
return 0
else
if @h.length == 0 && k != @num.length
return @num[-1]/@num[@num.length-k-1]
elsif @h.length == 0 && k == @num.length
return @num[-1]
else
if @h[0] < @num.length - k
if @num.length - k - 1 == @h[0]
return @num[-1]
else
return @num[-1]/@num[@num.length-k-1]
end
else
return 0
end
end
end
end
end
# Your ProductOfNumbers object will be instantiated and called as such:
# obj = ProductOfNumbers.new()
# obj.add(num)
# param_2 = obj.get_product(k)
最后就让13深藏于心吧