擅长:python、mysql、java
<p>是合并排序?哦,那不难。只要把这两张单子倒过来,比较一下每一对,然后找出输家。一旦其中一个列表为空,剩余的列表将按升序排序,并与整个结果进行比较,因此只需将其连接起来。在</p>
<pre><code>from operator import itemgetter as iget
def merge(lst1, lst2):
lst1, lst2 = lst1[::-1], lst2[::-1]
# popping from the end is MASSIVELY better than popping from the front
result = []
while True:
if not all([lst1, lst2]): # one list is empty:
return result + lst1[::-1] + lst2[::-1] # so add them both and return
lst_to_pop_from = min(lst1,lst2, key=iget(-1))
# gets the list with the smallest final element
result.append(lst_to_pop_from.pop())
</code></pre>
<p>在</p>
^{pr2}$