简体   繁体   English

从数组的末尾找到第 n 个元素

[英]Find an nth element from the end of an array

i looked at this challenge from codesignal (nthelementfromtheend) and put my code (below) in a test site我从 codesignal (nthelementfromtheend) 中查看了这个挑战并将我的代码(如下)放在测试站点中

function nthElementFromTheEnd(l, n) {
if (n > l.length){
    return -1;
}else{

// console.log();
let index = l.length - n;
// console.log(index);
// console.log(l[index]);
return l[index];
}
}

let l = [1, 2, 3, 4];
let n=7;
nthElementFromTheEnd(l, n);

results seem to pass the test site, but not codesignal.结果似乎通过了测试站点,但不是codesignal。

open links below in new tab在新标签中打开下面的链接

challenge挑战

tester测试员

array length 数组长度

You need to analyze the input that is coming into the function.您需要分析进入函数的输入。 l represents a singly-linked list. l代表一个单链表。 This doesn't exist natively in JavaScript, but it has been re-created using an object, as the comment describes:这在 JavaScript 中本身并不存在,但它已使用对象重新创建,如注释所述:

// Singly-linked lists are already defined with this interface:
function ListNode(x) {
    this.value = x;
    this.next = null;
}

In the first test, the input that comes to the function looks like this:在第一个测试中,函数的输入如下所示:

ListNode {
    value: 1,
    next: ListNode {
        value: 2,
        next: ListNode {
            value: 3,
            next: null
        }
    }
}

So this is not as simple as returning a particular index from an array, because the function is not receiving an array but an object.所以这不像从数组中返回特定索引那么简单,因为函数接收的不是数组而是对象。 You have to navigate the data structure continually checking for next values.您必须在数据结构中不断地检查next值。 There are probably more efficient ways to do this, but here's an example that at least passes the 8 sample tests:可能有更有效的方法来做到这一点,但这里有一个至少通过 8 个样本测试的例子:

function nthElementFromTheEnd(l, n) {
    let values = [];
    let node = l;

    while (node) {
        values.push(node.value);
        node = node.next;
    }

    let len = values.length;

    if (n > len) {
        return -1;
    } else {
        return values[len-n];
    }
}

The trick here is in the comment indicating the interface of a singly-linked lists.这里的技巧是在指示单链表接口的注释中。

// Singly-linked lists are already defined with this interface:
// function ListNode(x) {
//   this.value = x;
//   this.next = null;
// }
//

So you need to use l.next and l.value to navigate and get values from the linked list.所以你需要使用l.nextl.value来导航并从链表中获取值。

Here is a possible solution (not optimized):这是一个可能的解决方案(未优化):

function nthElementFromTheEnd(l, n) {
    // find the length of the linked list
    let len = 1;
    let c = l;
    while (c.next) {
        len++;
        c = c.next;
    }

    if (n > len) {
        return -1
    }
    else {
        // iterate linked list and get desired value (len-n)
        let i = 0;
        while (i < len-n){
            l = l.next;
            i++;
        }

        return l.value;
    }
}
function nthElementFromTheEnd(l, n) {
var input = l;
var rev= input.reverse();
   let value = rev[n-1];
   if(value){
     return value;
   }
   else
    return -1;
}

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

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