Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在云层上跳跃HackerRank_Javascript - Fatal编程技术网

Javascript 在云层上跳跃HackerRank

Javascript 在云层上跳跃HackerRank,javascript,Javascript,我想做一个函数,返回一个数组,其中包含完成另一个数组所需的步数,但有一个小条件,我只想在0上执行我的步骤,这意味着如果我有数组c=[0,0,0,1,0,0,1,0]它将把前三个0作为一个步骤,但是如果我只有一个0,正如你在数组末尾看到的,它将是一个步骤,因此对于这个数组,它将需要4个步骤来完成它(0,0,0)(0)(0),因为你看到它将忽略(1) 如果您想要更好的描述,这是测试的链接 var数组=[0,0,0,1,0,0,1,0] var stepsArray=[]; 函数跳线闭合(c){

我想做一个函数,返回一个数组,其中包含完成另一个数组所需的步数,但有一个小条件,我只想在0上执行我的步骤,这意味着如果我有数组c=[0,0,0,1,0,0,1,0]它将把前三个0作为一个步骤,但是如果我只有一个0,正如你在数组末尾看到的,它将是一个步骤,因此对于这个数组,它将需要4个步骤来完成它(0,0,0)(0)(0),因为你看到它将忽略(1)

如果您想要更好的描述,这是测试的链接

var数组=[0,0,0,1,0,0,1,0]
var stepsArray=[];
函数跳线闭合(c){
对于(变量i=0;i控制台日志(结果)
刚刚为
I
的索引在您的解决方案中添加了2个约束,并删除了不需要的条件:

我在Hackerrank上测试了我的解决方案:

看看这个:

“严格使用”;
常数fs=要求('fs');
process.stdin.resume();
进程标准设置编码('utf-8');
让inputString='';
设currentLine=0;
process.stdin.on('data',inputStdin=>{
inputString+=inputStdin;
});
process.stdin.on('end',_979;=>{
inputString=inputString.replace(/\s*$/,“”)
.split(“\n”)
.map(str=>str.replace(/\s*$/,“”));
main();
});
函数readLine(){
返回inputString[currentLine++];
}
//完成下面的JumpingOnCluds功能。
函数跳线闭合(c){
var stepsArray=[];
设i=0;
而(i}
刚刚对
I
索引的解决方案添加了2个约束条件,并删除了不需要的条件:

我在Hackerrank上测试了我的解决方案:

看看这个:

“严格使用”;
常数fs=要求('fs');
process.stdin.resume();
进程标准设置编码('utf-8');
让inputString='';
设currentLine=0;
process.stdin.on('data',inputStdin=>{
inputString+=inputStdin;
});
process.stdin.on('end',_979;=>{
inputString=inputString.replace(/\s*$/,“”)
.split(“\n”)
.map(str=>str.replace(/\s*$/,“”));
main();
});
函数readLine(){
返回inputString[currentLine++];
}
//完成下面的JumpingOnCluds功能。
函数跳线闭合(c){
var stepsArray=[];
设i=0;
而(i}
第一个问题是,您正在使用全局变量
stepsArray
跟踪步骤,并且没有在每次调用
jumpingoncluds
之后或之前清除步骤,这意味着
stepsArray
将累积每个测试的步骤

第二,当您得到一个双
0
步骤时,您需要增加
i

以下是您的方法的修改工作版本:

var数组=[0,0,0,1,0,0,1,0];
函数跳线闭合(c){
var计数=0;
对于(变量i=0;iconsole.log(result);
第一个问题是使用全局变量
stepsArray
跟踪步骤,并且在每次调用
jumpingoncluds
之后或之前没有清除步骤,这意味着
stepsArray
将累积每个测试的步骤

第二,当您得到一个双
0
步骤时,您需要增加
i

以下是您的方法的修改工作版本:

var数组=[0,0,0,1,0,0,1,0];
函数跳线闭合(c){
var计数=0;
对于(变量i=0;i
函数跳转关闭命令(c){
var n=0;
对于(变量i=0;i
函数跳线闭合(c){
var n=0;
对于(变量i=0;i
最简单的方法

const recCloud=clouds=>{
如果(长度<4)
返回1;
其他的
返回1+recCloud(clouds.slice(clouds[2]==0?2:1));
};
甚至

const recCloud=clouds=>{
返回clouds.length<4?1:1+recCloud(clouds.slice(clouds[2]==0?2:1));
};
最简单的方法

const recCloud=clouds=>{
如果(长度<4)
返回1;
其他的
返回1+recCloud(clouds.slice(clouds[2]==0?2:1));
};
甚至

const recCloud=clouds=>{
返回clouds.length<4?1:1+recCloud(clouds.slice(clouds[2]==0?2:1));
};
const jumpingOnClouds=(云)=>{
让jumpCount=0;
设currentCloud=0;
while(currentCloudconst recCloud = clouds => {

    if(clouds.length < 4)
        return 1;
    else
        return 1 + recCloud(clouds.slice(clouds[2] == 0 ? 2 : 1));
};
const recCloud = clouds => {
    return clouds.length < 4 ? 1 : 1 + recCloud(clouds.slice(clouds[2] == 0 ? 2 : 1));
};
const jumpingOnClouds = (clouds) => {

    let jumpCount = 0;
    let currentCloud = 0;

    while (currentCloud < clouds.length-1) {

        // There are only two ways to jump. Either jump two clouds away or one 
        // cloud away from the current position

        // Check if next 2 clouds are "cumulus" which values are 0s
        // If they are cumulus then jump to 2 clouds away from the current cloud
        // else jump 1 position away from the current cloud
        // Since you jump on both cases, you just have to increment the number 
        // of jumps by 1.

        if (clouds[currentCloud + 2] == 0) {
            currentCloud += 2;
        } else {
            currentCloud ++;
        }
        jumpCount++;
    }
    return jumpCount;
}
int jumpingOnClouds(vector<int> c) 
  {
    int n;
    n=c.size();
    int count = 0;
     for (int i = 0; i < n-1; i++)
    {
     count++;
     if (i<n-2 && c[i+2]==0) i++;
    }
     return count;
  }
static int jumpingOnClouds(int[] c) {
    int jump = 0;
    int pos = 0;
    while (pos < c.length) {
        if (pos + 2 < c.length && c[pos + 2] == 0) {
            pos = pos + 2;
            jump++;
        } else if (pos + 1 < c.length && c[pos + 1] == 0) {
            pos = pos + 1;
            jump++;
        } else
            break;
    }
    return jump;
}
        int jumpcount=0;
        for(int i=0; i<c.length-1;){
            if(c[i]==0){
                if(i+2<c.length && c[i+2]==0){
                    jumpcount++;
                    i+=2;
                }else if(c[i+1]==0){
                    jumpcount++;
                    i++;
                }
            }
        }
int jumpingOnClouds(int c_count, int* c) {
    int i=0,jump=0;
        
    while(i<c_count-1){
        
        if(*(c+i+1)==0 && *(c+i+2)==0){
            jump++; i+=2;
        }
        else if((*(c+i+1)==0 && *(c+i+2)==1) || *(c+i+1)==0){
            jump++; i++;
        }
        else if(*(c+i+1)==1 && *(c+i+2)==0){
            jump++; i+=2;
        }
        else 
            break;
    }
    return jump;
}
static int jumpingOnClouds(int[] c) {
        int num_jumps = 0;
        int i = 0;
        while (i < c.length - 1) {
            if (i + 2 == c.length || c[i + 2] == 1) {
                i++;
                num_jumps++;
            } else {
                i += 2;
                num_jumps++;
            }
        }
    return num_jumps;
    }
    function jumpingOnClouds($c) {
        $count = 0;

        for ($i = 1; $i < count($c); $i++) {
            $a =  $c[$i];
            if ($a == 0) {
                if ($a === $c[$i + 1] && $a === $c[$i - 1]) {
                    $count++;
                    $i++;  
                } else {
                    $count++;
                }
            }
        }
        return $count;
    }