MCPcopy Create free account
hub / github.com/HuberTRoy/leetCode / mergeArray

Function mergeArray

Array/MergeArray.py:63–95  ·  view source on GitHub ↗

这个测试数据的结构是我自己写的,所以第一步是打散数组。 1. 根据第一个字符出现的位置进行排序。 2. 迭代,记录i的头,记录i的末尾,末尾与下一个i的头做比较,若前者记录的大或相等则末尾替换为两者中较大的一个。 3. 不大的情况添加到结果中,并将头尾替换为此时的数据。 4. 最后一轮迭代在添加一次。

(sentences)

Source from the content-addressed store, hash-verified

61
62
63def mergeArray(sentences):
64
65 """
66 这个测试数据的结构是我自己写的,所以第一步是打散数组。
67 1. 根据第一个字符出现的位置进行排序。
68 2. 迭代,记录i的头,记录i的末尾,末尾与下一个i的头做比较,若前者记录的大或相等则末尾替换为两者中较大的一个。
69 3. 不大的情况添加到结果中,并将头尾替换为此时的数据。
70 4. 最后一轮迭代在添加一次。
71
72 """
73
74 # 打散,相当于原题给的数据中的读入。
75 _sentences = sorted([y for x in test for y in x], key=lambda x: x[0])
76
77 if not _sentences:
78 return []
79
80 result = []
81
82 head = _sentences[0][0]
83 tail = _sentences[0][1]
84 length = len(_sentences)
85 for x in range(1, length):
86 i = _sentences[x]
87 if tail >= i[0]:
88 tail = max(tail, i[1])
89 else:
90 result.append([head, tail])
91 head = i[0]
92 tail = i[1]
93
94 result.append([head, tail])
95 print(result)
96
97mergeArray(test)
98

Callers 1

MergeArray.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected