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 ...
随机推荐
- [OpenCV] 4、图像叠加 addWeighted
>_<" 这次主要是图像叠加的基本操作: >_<" cv::addWeighted(imageROI, 1.0, logo, 0.6, 0, imageRO ...
- 使用Nito.AsyncEx实现异步锁
Lock是常用的同步锁,但是我们无法在Lock的内部实现异步调用,比如我们无法使用await. 以下面的代码为例,当你在lock内部使用await时,VS会报错提醒. 最简单的解决办法就是使用第三方的 ...
- [Python] 中文编码问题:raw_input输入、文件读取、变量比较等str、unicode、utf-8转换问题
最近研究搜索引擎.知识图谱和Python爬虫比较多,中文乱码问题再次浮现于眼前.虽然市面上讲述中文编码问题的文章数不胜数,同时以前我也讲述过PHP处理数据库服务器中文乱码问题,但是此处还是准备简单做下 ...
- Step by Step:Linux C多线程编程入门(基本API及多线程的同步与互斥)
介绍:什么是线程,线程的优点是什么 线程在Unix系统下,通常被称为轻量级的进程,线程虽然不是进程,但却可以看作是Unix进程的表亲,同一进程中的多条线程将共享该进程中的全部系统资源,如虚拟地址空间, ...
- atitit. js 跨界面 页面 web cs 传值方法总结
atitit. js 跨界面 页面 web cs 传值方法总结 #--需求 js #---两个方法: 直接传跟跟间接传递... 1.直接传跟new form(param) web使用url方 ...
- Maven学习总结(七)——eclipse中使用Maven创建Web项目
一.创建Web项目 1.1 选择建立Maven Project 选择File -> New ->Project,如下图所示:
- WindowsPhone8拍照功能实现简介
WindowsPhone作为一款智能手机操作系统,支持APP中拍照是必不可少的,目前在WP8上的拍照主要有以下三种途径: 1.使用CameraCaptureTask: 2.使用PhotoCamera类 ...
- 26数据查询的各种小玩法-select 下(必学)-天轰穿sqlserver视频教程
大纲:简单查询-选择数据列,使用字符串,改变列标题,使用数据运算,使用ALL语DISTINCT关键字,使用TOP关键字,排序 优酷超清地址,为了冲优酷的访问量,所以这里只放优酷的地址了,其实其他网站还 ...
- INFO - InstallShield中的InstallScript工程Setup.exe /s的使用细节
在InstallShield的各种工程类型中,Basic MSI工程Build出的安装包基于Windows Installer标准,所以默认就支持静默安装(至于如何静默安装,请自行补充相关知识).而对 ...
- 【Android】android中Invalidate和postInvalidate的区别
Android中实现view的更新有两组方法,一组是invalidate,另一组是postInvalidate,其中前者是在UI线程自身中使用,而后者在非UI线程中使用. Android提供了Inva ...