日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Introduction to algrithms exercise2.3-7

發布時間:2024/4/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Introduction to algrithms exercise2.3-7 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。