发现如果两个在pre-排序的数组和元素等于某个值 [英] Finding if two elements in a pre-sorted array sum to equal a certain value

查看:118
本文介绍了发现如果两个在pre-排序的数组和元素等于某个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一门功课的问题,我有一些困难,创造一个为O(n * LOGN)解决方案。我需要编写一个函数,接受pre排序阵列和一个值来搜索。那么我需要找到如果数组总和的任意两个元素等于该值。

I'm working on a homework problem and I'm having some difficulties creating a O(n*logn) solution. I need to write a function that takes a pre-sorted array and a value to search for. I then need to find if any two elements of the array sum to equal that value.

我需要创建两个O(n)和O(​​N * LOGN)算法这一点。

I need to create both O(n) and O(n*logn) algorithms for this.

在为O​​(n),很容易产生;但是,我有创造的O(N * LOGN)算法,而不在一些无端code实际上并没有解决问题有助于增加困难。如果任何人都可以给我什么,我可能会丢失这将是AP preciated一些指点。

The O(n) was easy to create; however, I am having difficulties creating the O(n*logn) algorithm without adding in some gratuitous code that doesn't actually help in solving the problem. If anyone could give me some pointers on what I might be missing it would be appreciated.

推荐答案

在开始的第一个元素,并依次进入。而,搜索使用二进制搜索第二元件。

Start at the first element, and go sequentially. While that, search for the second element using binary search.

这篇关于发现如果两个在pre-排序的数组和元素等于某个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆