Java的:如何找到孔在一维图? [英] Java: How to find hole in a one dimensional graph?

查看:103
本文介绍了Java的:如何找到孔在一维图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已经问过,但我不能做出什么出格的答案,所以我希望有人能够解释这个给我一个非常简单的方法......

I know this has asked before but I couldn't make anything out of the answers so I hope someone will be able to explain this to me in a really easy way....

问题:

一个人站立在墙前面有一个无限长。在这堵墙的另一边是一个小镇,他正试图去。某处在墙门的家伙可以去左边或右边找到它。

A man is standing in front of a wall with an infinite length. On the other side of the wall is a town he's trying to get to. Somewhere in the wall is a gate and the guy can either go left or right to find it.

我需要写一个算法具有线性运行时间,我只是没能想出解决办法...任何帮助是极大的AP preciated!

I need to write an algorithm with a linear runtime and I am just not able to figure this out... Any help is greatly appreciated!

推荐答案

不知道如何暗示了答案,而不只是给它拿走。

Not sure how to hint the answer without just giving it away.

您知道你不能走无限左,如果失败向右走,直到你找上了门。这就是出去。

You know you can't go "infinitely left", then if that fails go right until you find the door. That's out.

于是找上了门可言,你将不得不去留给了一下,然后转身回到起点,向右走一点,重复逐渐增大位。

So to find the door at all, you're going to have to go left for a bit, then turn and go back to the start, go right a bit, repeat with larger and larger bits.

接下来的问题是,你能想到的办法,以增加的位的大小,使得整个运行时间,即所有的位到一个足够大的找上了门总,是线性的?

The question then is, can you think of a way to increase the size of the "bits" such that the overall runtime, i.e. the total of all the "bits" up to the one that's big enough to find the door, is linear?

您已经在注释中说,如果你每次增加1,那么复杂度太高(为O(n ^ 2)实际上,没有指数,但仍然过大)。因此,寻找一个更好的发展,一个将导致总对ING来回-ING少。

You've already said in a comment that if you increase by 1 each time, then the complexity is too high (O(n^2) actually, not exponential, but still too big). So look for a better progression, one that will result in less total to-ing and fro-ing.

这篇关于Java的:如何找到孔在一维图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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