简体   繁体   English

C++ 迭代器和向量

[英]C++ Iterators and Vectors

So im solving a hackerrank challenge called "Jumping on the clouds" and it involves taking the least amount of steps to get to the end.所以我解决了一个叫做“在云端跳跃”的黑客挑战,它涉及采取最少的步骤来结束。 Ill summarize the problem:总结一下问题:

Emma is playing a new mobile game that starts with consecutively numbered clouds.艾玛正在玩一个新的手机游戏,它以连续编号的云开始。 Some of the clouds are thunderheads and others are cumulus.有些云是雷雨云,有些则是积云。 She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2 .她可以在任何积云的数字等于当前云的数量加 1 或 2 上跳跃。 She must avoid the thunderheads.她必须避开雷霆。 Determine the minimum number of jumps it will take Emma to jump from her starting postion to the last cloud.确定艾玛从她的起始位置跳到最后一朵云所需的最少跳跃次数。 It is always possible to win the game.总是有可能赢得比赛。

The "list" of clouds is given in a vector of 0's and 1's.云的“列表”以 0 和 1 的向量给出。 0's are "safe" 1's Emma can not jump to. 0 是“安全的” 1 的 Emma 不能跳到。

The function we receive is a vector of 0's and 1's like so:我们收到的函数是一个由 0 和 1 组成的向量,如下所示:

int jumpingOnClouds(vector<int> c) {


}

So I thought to myself "ok"...well since it's always possible to win the game we should ALWAYS check 2 spaces ahead (for a 1) and if not just jump 1 position).所以我心想“好吧”……好吧,因为总是有可能赢得比赛,所以我们应该始终检查前面的 2 个空格(对于 1),如果不只是跳到 1 个位置)。

So I wrote up something like so (Im still refreshing on C++ so my code is probably not great):所以我写了这样的东西(我仍然对 C++ 感到耳目一新,所以我的代码可能不是很好):

int jumpingOnClouds(vector<int> c) {
    int jumps = 0;
    vector<int>::iterator it = c.begin();
    vector<int>::iterator it2 = c.begin();

    while(it != c.end())
    {
        it2 = next(it,2);
        if(*it2 == 0){
            jumps++;
            advance(it, 2);
        }
        else {
            jumps++;
            advance(it,1);
        }
    }
    return jumps;
}

However this always ends in a segfault.然而,这总是以段错误结束。 I am not super familiar with iterators in general and created this just from checking the iterator documentation.我对迭代器一般不太熟悉,只是通过检查迭代器文档来创建它。 My "guess" is that im going 2 past the end if the last element is 0. The first input list is 0 0 1 0 0 1 0 so it seems like it would jump once to pos1, then jump twice to pos3, then once to pos4, then finally jump twice to the last pos then stop.我的“猜测”是,如果最后一个元素是 0,我会越过结尾 2。第一个输入列表是0 0 1 0 0 1 0所以它似乎会跳一次到 pos1,然后跳两次到 pos3,然后一次到 pos4,然后最后跳两次到最后一个 pos 然后停止。 So it doesn't seem like initially it should fail.所以它最初似乎不应该失败。

Im sure im missing something obvious though.我确定我错过了一些明显的东西。

it2 = next(it,2)

it2 may be past the end of the vector, once you dereference it with *it2, boom segfault. it2 可能会超过向量的末尾,一旦您使用 *it2 取消引用它,boom segfault。

Don't want to give you too big a hint to the solution, but there is a flaw in your logic.不想给你太大的解决方案提示,但你的逻辑有缺陷。

This is a path searching problem, I'd use recursion.这是一个路径搜索问题,我会使用递归。 You need to try all valid paths through the array.您需要尝试通过数组的所有有效路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM