Describe a (nlgn)-time algorithm that, given a set S of integers and another integer x, determings whether or not there exist two elements in S whose sum is exactly x.
汉语:描述一个nlgn复杂度的算法:给定一个n个数字的集合,和另一个数字x,判断集合中是否存在两个数字,它们的和是x
题目分析及伪代码实现
        
如果算法复杂度为n^2,那么很好设计比如可以采用以下算法
TestExistence(A,begin,end,x)
 for i<-1 to n  
      for j<-i+1 to n
             if a[i]+a[j]=x
              then return i,j
return 0,0
现在要求算法复杂度为nlgn.通过本章的学习,我们知道折半查找和归并法爬序的算法复杂度为nlgn。所以我们猜想这个题目的算法可能和这两种算法挂上钩。
伪代码实现
TestExsistence(S,begin,end,x)
1. MergeSort(S,begin,end);
2.Create S' 
for i<-1 to n
S'[i]=x-S[i];
3.MergeSort(S',begin,end);
4.purge S,S' seperately;
 4.1 TestEqual(x,y);//比较两个元素是否相等
5. if the merged output contains two consecutive equal value ,the answer is true.
控制此算法复杂度数量级的关键在于控制purge 部分的算法复杂度。
未完待续。

转载于:https://www.cnblogs.com/finallyliuyu/archive/2009/08/12/1544432.html

Introduction to algrithms exercise2.3-7相关推荐

  1. Blender 3.0基础入门学习教程 Introduction to Blender 3.0

    成为Blender通才,通过这个基于项目的循序渐进课程学习所有主题的基础知识. 你会学到什么 教程获取:Blender 3.0基础入门学习教程 Introduction to Blender 3.0- ...

  2. 网络增强现实开发简介 Introduction to Web AR development

    搭配webXR.mindAR.three.js和tensorflow.js 你会学到: 获得构建不同类型的网络增强现实应用程序的实践经验,包括图像效果.人脸效果和世界效果 获得关于增强现实如何在网络浏 ...

  3. ZBrush全面入门学习教程 Schoolism – Introduction to ZBrush

    ZBrush全面入门学习教程 Schoolism – Introduction to ZBrush ZBrush全面入门学习教程 Schoolism – Introduction to ZBrush ...

  4. 视频色彩校正简介 Introduction to Video Color Correction

    视频色彩校正简介 Introduction to Video Color Correction 视频色彩校正简介 Introduction to Video Color Correction MP4 ...

  5. [转]Introduction of iSCSI Target in Windows Server 2012

    Introduction of iSCSI Target in Windows Server 2012 源地址:http://blogs.technet.com/b/filecab/archive/2 ...

  6. MS UI Automation Introduction

    MS UI Automation Introduction 2014-09-17 MS UI Automation是什么 UIA架构 UI自动化模型 UI自动化树概述 UI自动化控件模式概述 UI 自 ...

  7. 音频(3):iPod Library Access Programming Guide:Introduction

    Next Introduction 介绍 iPod库访问(iPod Library Access)让应用程序可以播放用户的歌曲.有声书.和播客.这个API设计使得基本播放变得非常简单,同时也支持高级的 ...

  8. 2018-3-25论文(Whale Optimizer Algorithm)+(Gery Wolf Optimizer)笔记二---Introduction 对比

    代码以及文论的来源: Seyedali Mirjalili http://www.alimirjalili.com/Projects.html 感谢作者!!! 2014年GWO Introductio ...

  9. 北航云计算公开课 01 Introduction to Cloud Computing

    北航云计算公开课 01 Introduction to Cloud Computing_邓侃_新浪博客 北航云计算公开课 01 Introduction to Cloud Computing 受北航软 ...

最新文章

  1. 利用Injection插件加快Xcode编译速度
  2. pytorch中的torch.nn.LSTM解析
  3. 【项目管理】ITTO-采购管理
  4. 虚拟服务器的磁盘,虚拟服务器磁盘扩容
  5. 【Python】Flask框架系列(三):session和cookie操作、get/post请求、钩子函数
  6. 计算机PPT03,南京大学计算机网络课件03.ppt
  7. lru算法c语言实现单链表,操作系统之LRU算法 C语言链表实现
  8. 过滤代码中的html标签
  9. 【图像处理】美图秀秀使用技巧:抠图、透明、改色、教程
  10. UML基础(八)--构件图
  11. linux tomcat6安装及配置
  12. BUG记录:org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is no
  13. 武汉东湖学院计算机科学学院团委,【计算机科学学院】
  14. 2019年,转行到互联网行业,还有必要么?
  15. [转]IE6下的CSS BUG枚举
  16. CSS样式之块元素行内元素
  17. web项目没有小蓝点
  18. 李娜大骂老公姜山的内幕真相(图)
  19. 科脉如何修改服务器名字,科脉云商助手服务器设置
  20. Z3 Solver中Tactic的使用

热门文章

  1. CF 295A - Greg and Array 差分數列
  2. 洛谷 2585 [ZJOI2006]三色二叉树——树形dp
  3. [转]application.properties详解 --springBoot配置文件
  4. 转---谈谈HTTP协议中的短轮询、长轮询、长连接和短连接
  5. 重载赋值运算符含动态申请
  6. Android自定义控件之仿汽车之家下拉刷新
  7. android获取string.xml的值
  8. Android RecyclerView详解
  9. android textView调整字体的间距和行间距
  10. android 访问https服务器