今天是11月的第一个周末,我安静的在家休息,又选择了一道leetcode设计题,感觉很有意思,由于调用方法不超过400次,可以尽情发挥
2590. 设计一个待办事项清单
class TodoList
def initialize()
@h = {}
@cnt = 0
end
=begin
:type user_id: Integer
:type task_description: String
:type due_date: Integer
:type tags: String[]
:rtype: Integer
=end
def add_task(user_id, task_description, due_date, tags)
unless @h.has_key?(user_id)
@cnt += 1
@h[user_id] = {}
@h[user_id][task_description] = {}
@h[user_id][task_description]["due_date"] = due_date
@h[user_id][task_description]["tags"] = tags
@h[user_id][task_description]["id"] = @cnt
@h[user_id][task_description]["completed"] = false
return @cnt
else
@cnt += 1
@h[user_id][task_description] = {}
@h[user_id][task_description]["due_date"] = due_date
@h[user_id][task_description]["tags"] = tags
@h[user_id][task_description]["id"] = @cnt
@h[user_id][task_description]["completed"] = false
return @cnt
end
end
=begin
:type user_id: Integer
:rtype: String[]
=end
def get_all_tasks(user_id)
if @h.has_key?(user_id)
return @h[user_id].select {|k,v| @h[user_id][k]["completed"] == false}.keys.sort_by {|it| @h[user_id][it]["due_date"]}
else
return []
end
end
=begin
:type user_id: Integer
:type tag: String
:rtype: String[]
=end
def get_tasks_for_tag(user_id, tag)
if @h.has_key?(user_id)
return @h[user_id].select {|k,v| @h[user_id][k]["completed"] == false && @h[user_id][k]["tags"].include?(tag)}.keys.sort_by {|it| @h[user_id][it]["due_date"]}
else
return []
end
end
=begin
:type user_id: Integer
:type task_id: Integer
:rtype: Void
=end
def complete_task(user_id, task_id)
if @h.has_key?(user_id)
m = @h[user_id].select {|k,v| @h[user_id][k]["id"] == task_id}.keys[0]
if m != nil
@h[user_id][m]["completed"] = true
end
end
end
end
# Your TodoList object will be instantiated and called as such:
# obj = TodoList.new()
# param_1 = obj.add_task(user_id, task_description, due_date, tags)
# param_2 = obj.get_all_tasks(user_id)
# param_3 = obj.get_tasks_for_tag(user_id, tag)
# obj.complete_task(user_id, task_id)