[問題] 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
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.228.105.126
※ 文章網址: https://www.ptt.cc/bbs/Ruby/M.1540132253.A.EF1.html
推
10/21 23:10,
6年前
, 1F
10/21 23:10, 1F
推
10/21 23:13,
6年前
, 2F
10/21 23:13, 2F
→
10/21 23:14,
6年前
, 3F
10/21 23:14, 3F
→
10/21 23:15,
6年前
, 4F
10/21 23:15, 4F
推
10/22 02:42,
6年前
, 5F
10/22 02:42, 5F
→
05/16 13:07,
6年前
, 6F
05/16 13:07, 6F
推
05/17 13:35,
6年前
, 7F
05/17 13:35, 7F
Ruby 近期熱門文章
PTT數位生活區 即時熱門文章