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 ...
随机推荐
- [Java Web] 1、Web开发初识——一大堆历史和技术名词
LZ前言 LZ最近发现网络真是个神奇的东西,以前做的好玩的只能自娱自乐(或者说顾影自怜),现在只要发一个帖子,写一个博客,很快能引来一大群小伙伴的围观(有时候还能遇见几个大牛给个战略性的指导)...L ...
- 西安.NET俱乐部群 推广代码
CSS: .gallery-item { display:inline-block; margin: 15px; } 个人签名: <div class="gallery&quo ...
- 使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
本周作业的主要内容就是采用gcc嵌入汇编的方式调用system call. 系统调用其实就是操作系统提供的服务.我们平时编写的程序,如果仅仅是数值计算,那么所有的过程都是在用户态完成的,但是我们想将变 ...
- Django基础——Model篇(三)
一 Django ORM中的概念 ORM —— 关系对象映射,是Object Relational Mapping的简写,是用来简化数据库操作的框架 Django ORM遵循Code Frist原则, ...
- 生成读取相关连接的物理地址的lib(动态导入库)和dll(动态链接库)
一.导出相关dll库 将原先的CmdInfoToPipe.h class后加入关键字 __declspec(dllexport) #ifndef NETINFO_CMDINFOTOPIPE_H_ #d ...
- 详解Bootstrap导航组件
在bootstrap框架中将导航独立出来成为一个导航组件,根据不同的版本,可以找到相应的源码: LESS: navs.less SASS: _navs.scss 标签形导航,也称选项卡导航 标签形 ...
- ATL字符串转换宏
有比MultiByteToWideChar和WideCharToMultiByte更简单的字符串转换宏,你相信吗?头文件 d:\program files\microsoft visual studi ...
- php: zend server 安装及相关配置
运行安装文件(ZendServer-CE-php-5.3.2-5.0.1-Windows_x86.exe)开始安装,选项请参照我的选择. 这里不做改动,维持默认选择即可 点击Browse按钮更改安装目 ...
- mysqld.exe 占了400M内存的问题
最近遇到了服务器总是停机的问题,虽然它自己只有2G的内存,不过实际部署的应用访问量非常小,也不至于2G就不够用,所以开始了给服务器瘦身的计划. 看着任务管理器里面的各个进程,发现吃内存最厉害的是mys ...
- .net下BerkeleyDB操作封装C#版(附单元测试)
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System ...