贊助廠商

娛樂城推薦

首頁

刊登資訊

  • 刊登者:匿名
  • 時間:2021-06-09 07:40:09

尚未解答Ruby- LeetCode 472. Concatenated Words

Ruby- LeetCode 472. Concatenated Words

會 time limit exceeded,求高手指點



def find_all_concatenated_words_in_a_dict(words)
return [] if words.count <= 1 || words.count > 10000 || words.map(&:to_s).inject(&:+).length > 600000

result = []

[*0...words.count].each do |i|
new_arr = words.dup
new_word = new_arr.delete(words[i]).dup

result << words[i] if concated_by_others?(new_word, new_arr)
end

return result
end

def concated_by_others?(str, arr)
[*1..str.length].each do |j|
if arr.include?(str.slice(0, j))
new_str = str.dup
new_str.slice!(str.slice(0, j))

return true if new_str.length.zero? or concated_by_others?(new_str, arr)
end
end

return false
end


--

0個答案 Ruby- LeetCode 472. Concatenated Words

其他問題

友站連結