首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有限制的公共整数上的合并对

具有限制的公共整数上的合并对
EN

Stack Overflow用户
提问于 2018-08-28 18:10:20
回答 1查看 299关注 0票数 8

我需要一种有效的方法来合并一个节点列表(整数对)。

只有当对中有一个公共数字,并且公共数字是位于第一个或最后一个位置时,合并才会发生(否则它就连接起来了)。

例如:

代码语言:javascript
复制
mylist = [[4, 3], [6, 3]]
merge_links(mylist) # should output [4, 3, 6]

另一个例子是:

代码语言:javascript
复制
mylist = [[4, 3], [6, 3], [6, 4]]
merge_links(mylist) 
# should output again [4, 3, 6] because both 6 and 4 allready exist in array.

还有另一个例子:

代码语言:javascript
复制
mylist = [[4, 3], [6, 3], [6, 4], [6, 2], [7, 4], [4, 9]]
merge_links(mylist) 
# should output [7, 4, 3, 6, 2]

代码语言:javascript
复制
# [4, 3] ✔ 
# [4, 3] + [6, 3] ✔ -> [4, 3, 6]
# [4, 3, 6] + [6, 3] ✘ both 6 and 3 exist in [4, 3, 6] 
# [4, 3, 6] + [6, 4] ✘ both 6 and 4 exist in [4, 3, 6]
# [4, 3, 6] + [6, 2] ✔ -> [4, 3, 6, 2]
# [4, 3, 6, 2] + [7, 4] ✔ -> [7, 4, 3, 6, 2]
# [7, 4, 3, 6, 2] + [4, 9] ✘ 4 is allready connected "7-4-3"!

目前我正在使用:

代码语言:javascript
复制
def merge_links(a, b):
    inter = np.intersect1d(a, b, True)
    a = set(a) - set(inter)
    b = set(b) - set(inter)
    n = np.unique(list(a) + list(inter) + list(b))
    return n

但是,对于上述限制,它不起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-28 19:57:28

以下代码的工作原理如下:

  1. 创建一个大小为旧列表+1的新列表(输出可以达到的最大大小)。
  2. 从列表中的第一对开始,它的第一个值在新列表的末尾,第二个值在新列表的开始。并使用python字典将它们标记为已访问的。
  3. 维护第一个和最后一个位置的两个索引,分别指向新列表的末尾和开始位置。
  4. 对其余的对进行迭代,如果对于对i,它的两个值在字典中都存在或不存在,则跳过它。否则,将未访问的值合并为第一个索引或最后一个索引的元素(取决于访问值的位置),并更新索引。注意,如果所访问的值不在第一个或最后一个索引(如果它位于中间的某个位置),则不会发生合并。
  5. 返回新listfirst索引的连接,以新liststart结束到最后一个索引。

注意:每个合并操作都采用O(1)。此外,如果您知道对值的范围,则可以使用布尔值数组而不是字典。

代码语言:javascript
复制
#The function takes a list of pairs as an argument
def merge_lst(lst):

  #Dictionary to check if value is already in array
  visited = dict()

  #Length of old list
  lst_len = len(lst)

  #New list will have at most lst_len+1 elements
  new_lst = [0]*(lst_len+1)

  #Put the first pair values in last and first elements of the new list repectively and mark them as visited
  new_lst[lst_len], new_lst[0] = lst[0][0], lst[0][1]
  visited[lst[0][0]], visited[lst[0][1]] = True, True

  #Maintain the positions of your first and last elements, which are the the last index and 0 respectively now
  first_index, last_index = lst_len, 0

  #Iterate on the rest of pairs
  for i in range(1, lst_len):

    #Check if pair[a, b] are already visited
    a_exists, b_exists = lst[i][0] in visited, lst[i][1] in visited

    #Skip if they both exist or don't exist
    if(a_exists == b_exists):
      continue

    #Assume a was the common one
    common, to_merge = lst[i][0], lst[i][1]

    #If b exists (b is the common not a), then just swap
    if(b_exists):
      common, to_merge = lst[i][1], lst[i][0]

    #If the common element is at the first index, the first element and index are updated
    if(new_lst[first_index] == common):
      first_index-=1
      new_lst[first_index] = to_merge
      visited[to_merge] = True

    #If the common element is at the last index, the last element and index are updated
    elif(new_lst[last_index] == common):
      last_index+=1
      new_lst[last_index] = to_merge
      visited[to_merge] = True

    #Else, the common element is somewhre in the middle (already connected)

  #Return concatenation of new_lst[first_index to the end] with new_lst[0 to the last_index] 
  return new_lst[first_index:lst_len+1]+new_lst[0:last_index+1]

此代码为您提到的所有测试用例提供了正确的输出。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52063999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档