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)
merge 实现的更多相关文章
- [算法]——归并排序(Merge Sort)
归并排序(Merge Sort)与快速排序思想类似:将待排序数据分成两部分,继续将两个子部分进行递归的归并排序:然后将已经有序的两个子部分进行合并,最终完成排序.其时间复杂度与快速排序均为O(nlog ...
- SQL 提示介绍 hash/merge/concat union
查询提示一直是个很有争议的东西,因为他影响了sql server 自己选择执行计划.很多人在问是否应该使用查询提示的时候一般会被告知慎用或不要使用...但是个人认为善用提示在不修改语句的条件下,是常用 ...
- Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- SQL Tuning 基础概述06 - 表的关联方式:Nested Loops Join,Merge Sort Join & Hash Join
nested loops join(嵌套循环) 驱动表返回几条结果集,被驱动表访问多少次,有驱动顺序,无须排序,无任何限制. 驱动表限制条件有索引,被驱动表连接条件有索引. hints:use_n ...
- Git 少用 Pull 多用 Fetch 和 Merge
本文有点长而且有点乱,但就像Mark Twain Blaise Pascal的笑话里说的那样:我没有时间让它更短些.在Git的邮件列表里有很多关于本文的讨论,我会尽量把其中相关的观点列在下面. 我最常 ...
- Merge 的小技巧
今天跟大家分享一下搬动数据使用Merge的方法. 有些时候,当我们做数据搬动的时候,有时候做测试啊,换对象啊,就会存在有时候外键存在,不知道怎么对应的关系.比如我现在有架构相同的两组table , A ...
- [LeetCode] Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [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 t ...
随机推荐
- Linux:Vim
模式介绍: Vim具备6种基本模式和5中派生模式. 普通模式 启动后的默认模式,用于:移动光标.删除文本等待,常用命令: dd:删除当前行. [number]dd:连续执行number对应次数的dd命 ...
- web app 禁用手机浏览器缓存方法
开发过web app的同学,特别是前端人员,都碰到这烦人的事情,JS或CSS代码改变,可手机浏览器怎么刷新都不更新,手机浏览器的缓存特别恶劣. 所以今天贴个方法解决这问题.记得,本地调试的时候贴上,上 ...
- C++ Primer 变量和基本类型
<C++ Primer 4th>读书摘要 基本上所有的语言都要提供下列特征: • 内置数据类型,如整型.字符型等. • 表达式和语句:表达式和语句用于操纵上述类型的值. • 变量:程序员可 ...
- Oracle 查询用户和删除用户
------------------------------- 一.查询用户命令: select username from dba_users; 示例: 二.删除用户命名: drop user 用户 ...
- lucene join解决父子关系索引
http://www.cnblogs.com/LBSer/p/4417074.html 1 背景 以商家(Poi)维度来展示各种服务(比如团购(deal).直连)正变得越来越流行(图1a), 比如目前 ...
- Atitit.mvc的趋势与未来attilax总结
Atitit.mvc的趋势与未来attilax总结 1. Mvc的分类 (服务端mvc vs客户端mvc)1 2. Mvc的趋势,从服务端mvc正在转向客户端mvc1 2.1. 更加完善的分离..h ...
- sqlserver自定义函数的创建与调用
sqlserver中有系统提供的函数,像avg.sum.getdate()等,用户还可以自定义函数. 用户自定义的函数包括:标量函数和表值函数,其中标量函数和系统函数的用法一样,表值函数根据主体的定义 ...
- Help Viewer 2.2 独立运行
"C:\Program Files (x86)\Microsoft Help Viewer\v2.2\HlpViewer.exe" /catalogName VisualStudi ...
- ArrayList/Vector的原理、线程安全和迭代Fail-Fast
疑问 * ArrayList是非线程非安全的,具体是指什么?具体会产生什么问题?* ArrayList的内部原理是什么?为什么可以动态扩容?* Vector是线程安全的,具体是如何实现的?为什么不再推 ...
- 通过sqlplus导出数据到csv
例子 [oracle@localhost ~]$ cat data.sqlset echo offset feedback offset linesize 100set pagesize 0set s ...