2016-06-09 46 views
-4

我在www.hackerrank.com挑战服务巷道https://www.hackerrank.com/challenges/service-lane。我非常仔细地检查了我的代码,但在某些情况下仍然是错误的。这里是我的代码(在Javascript)我的Hackerrank挑战服务巷道代码不起作用

function main() { 
    var n_temp = readLine().split(' '), i, s; 
    width = readLine().split(' '); 
    for(var a0 = 0; a0 < n_temp[1]; a0++){ 
     var i_temp = readLine().split(' '); s = 3; 
     for (i=i_temp[0];i<=i_temp[1];i++) {s = Math.min(s,width[i]); if (s == 1) break;} 
     console.log(s); 
    } 
} 

的挑战有号码的列表(存储在宽度,它们的值是1和3之间)和一些测试案例(包括起始号码:i_temp [0]和结尾数字:i_temp [1。],两者都包含在内)。任务是从i_temp [0]i_temp [1。]宽度中得到最小数字。
这是一个不起作用的案例:
Input
Output
代码有什么问题?它在某些情况下可以成功运行。感谢您的帮助:d

+2

有什么情况下它不起作用?你在控制台遇到什么错误? –

+0

我不知道。该网站只显示我的输入和输出。它没有显示我预期的输出。没有错误。 –

+1

'这段代码有什么问题?'我们不知道它的用途,那是错误的。 –

回答

0

您需要解析字符串Integer.Otherwise比较一些input.I可能会失败,下面已经测试并得到了成功。

var n_temp = readLine().split(' '), i, s; 
width = readLine().split(' '); 
for(var a0 = 0; a0 < n_temp[1]; a0++){ 
    var i_temp = readLine().split(' '); s = 3; 
    for (i=parseInt(i_temp[0]);i<=parseInt(i_temp[1]);i++) {s = Math.min(s,width[i]); if (s == 1) break;} 
    console.log(s); 
} 
+0

哦,你是对的:D谢谢。 –

0

试试这个:

function main() { 
    var N, n, path, i, j, it, res; 
    [N, n] = readLine().split(' ').map(Number); 
    var path = readLine().split(' ').map(Number); 
    for (var it = 0; it < n; it++) { 
     [i, j] = readLine().split(' ').map(Number); 
     res = Math.min.apply(null, path.slice(i, j+1)); 
     console.log(res); 
    } 
} 
+0

代码工作,因为我把第6-7行改为'console.log(Math.min.apply(null,width.slice(i_temp [0],++ i_temp [1]));'(no s,i因为这条线可能是错的,上面的行和我的代码中的6-7行有什么区别? –