merge 实现

  今天写了个小程序,做两个已经从小到大排序好的数据的merge。

要求: listA = (1, 3, 5, 10); listB = (4, 6, 12);listA 和listB都是排序由小到大的列表,元素个数不限。

写代码将listA 按照由小到大的顺序合并,不去重。

第一轮代码如下:

def merge(A, B):
    if len(A)==0:
        return B
    if len(B)==0:
        return A

    result = []
    for itemA in A:
        for itemB in B:
            if itemA < itemB:
                print itemA, "<", itemB,  ‘--------->insert itemA:‘, itemA
                result.append(itemA)

            else:
                print itemA, ">", itemB, ‘--------->insert itemB:‘, itemB
                result.append(itemB)        

    return result

Alist = [1, 2, 3, 7, 8]
Blist = [4, 6]

print merge(Alist, Blist)
                

可想而知,得到的结果自然是不正确的。

问题呢? 很显然,break 跳出了循环后,再进入的时候,还会从B的开始查起,这样有的元素就会多次被查到,而在insertB的时候,也会丢掉A的循环。最终的数据结果是,A的元素丢失,B的元素多次被插入。

那如何争取解决问题呢?

下面这个代码写的很繁复,但是实现了基本功能。

def merge(A, B):
    if len(A)==0:
        return B
    if len(B)==0:
        return A

    result = []
    lenA = len(A)
    lenB = len(B)
    currentAIndex = 0
    currentBIndex = 0

    for i in range(currentAIndex, lenA):
        for j in range(currentBIndex, lenB):
            if A[i] < B[j]:
                print A[i], "<", B[j],  ‘--------->insert itemA:‘, A[i]
                result.append(A[i])
                currentAIndex = currentAIndex+ 1
                break
            else:
                print A[i], ">", B[j], ‘--------->insert itemB:‘, B[j]
                result.append(B[j])
                currentBIndex = currentBIndex+ 1

    while currentAIndex <lenA:
        result.append(A[currentAIndex])
        currentAIndex = currentAIndex+ 1

    while currentBIndex <lenB:
        result.append(B[currentBIndex])
        currentBIndex = currentBIndex+ 1

    return result

Alist = [1, 7, 8]
Blist = [4, 6, 9, 10]
print merge(Alist, Blist)
时间: 2024-10-10 12:25:39

merge 实现的相关文章

归并排序(Merge Sort)

更详细的请看这篇博文:http://www.cnblogs.com/jingmoxukong/p/4308823.html 我只做个人学习和补充 基于分治法的排序,比较简单.个人感觉其核心是数组左右拆分之后类似队列的比较.类似两两合并之类的算法都可以参考 分为3个步骤,拆开为树结构遍历->向上合并 特别做了一张gif来更好的理解归并排序 左边集合比较通过就左边索引前进,右边集合比较通过则右边索引前进 比较结果放入结果数组中 最后如果多出一个就把这一个直接加入结果数组 为了易于学习,代码直接使用了

Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Subscribe to see which companies asked this question /** * Definition for singly-linked list. * struct ListN

LeetCode 21 Merge Two Sorted Lists

翻译 合并两个排好序的链表,并返回这个新链表. 新链表应该由这两个链表的头部拼接而成. 原文 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 代码 /** * Definition for singly-linked list. * struct ListNode

LeetCode --- 88. Merge Sorted Array

题目链接:Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements init

【leetcode】Merge k Sorted Lists

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 采用优先队列priority_queue 把ListNode放入优先队列中,弹出最小指后,如果该ListNode有下一个元素,则把下一个元素放入到队列中 1 /** 2 * Definition for singly-linked list. 3 * stru

oracle查询转换_view merge

oracle对于子查询的支持做的很好,oracle optimizer会对inline view进行query transfomation,即视图合并,不过也经常带来意想不到的问题.下面是一个inline view的merge的例子: 1, 创建临时表 1 create table test1 as select * from dba_objects; 2 create table test2 as select * from dba_objects; 2, 以下查询语句 select * fr

LeetCode记录之21——Merge Two Sorted Lists

算法和数据结构这东西,真的是需要常用常练.这道看似简单的链表合并题,难了我好几个小时,最后还是上网搜索了一种不错算法.后期复习完链表的知识我会将我自己的实现代理贴上. 这个算法巧就巧在用了递归的思想,按照常规方法也能求得,但是就未免太复杂了. Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the fir

leetcode - Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x)

leetcode || 56、 Merge Intervals

problem: Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. Hide Tags Array Sort 题意:给定数组区间,合并有覆盖或者相邻的区间 thinking: (1)一開始我想到用hash table的方法,开一个总区间跨度的数组.对于有区间覆盖的数

elasticsearch index 之merge

merge是lucene的底层机制,merge过程会将index中的segment进行合并,生成更大的segment,提高搜索效率.segment是lucene索引的一种存储结构,每个segment都是一部分数据的完整索引,它是lucene每次flush或merge时候形成.每次flush就是将内存中的索引写出一个独立segment的过程.所以随着数据的不断增加,会形成越来越多的segment.因为segment是不可变的,删除操作不会改变segment内部数据,只是会在另外的地方记录某些数据删