在Leetcode Palindrome问题(编号234)上,为什么我的JavaScript解决方案没有通过所有测试用例?
我正在研究回文问题,但 [1,2]
测试用例失败。下面是我的代码:
var isPalindrome = function(head) {
var listLength = head.length ? head.length : 0;
if (listLength === 0 || listLength === 1) return true;
var curNode = head[0];
var tailNode = head[listLength - 1];
if (listLength === 2) {
// if (curNode === tailNode) return true;
// return false;
return curNode === tailNode;
}
if (curNode !== tailNode) return false;
head.shift();
head.pop();
return (isPalindrome(head));
};
当我在设备上的 vscode 中运行测试用例时,我得到 [1,2]
的 false
(这是预期的结果),但上传的leetcode 上的版本未通过该测试并返回 true
。我不知道为什么。我的解决方案绝不是最好的解决方案,但我在本地尝试了一些测试,它似乎完成了工作。关于如何解决这个问题的任何建议,或者关于为什么我在 leetcode 上测试用例失败的见解?
最初我认为这是当 listLength 为 2 时我构建条件的方式,但我将其更改为我确信会起作用的东西,并且它没有改变结果。
编辑:链接到leetcode问题 https://leetcode.com/problems/palindrome-linked-list /
I'm working on the palindrome question, and failing the [1,2]
test case. Below is my code:
var isPalindrome = function(head) {
var listLength = head.length ? head.length : 0;
if (listLength === 0 || listLength === 1) return true;
var curNode = head[0];
var tailNode = head[listLength - 1];
if (listLength === 2) {
// if (curNode === tailNode) return true;
// return false;
return curNode === tailNode;
}
if (curNode !== tailNode) return false;
head.shift();
head.pop();
return (isPalindrome(head));
};
When I run the test case in vscode on my device, I get false
for [1,2]
(which is the expected result) but the uploaded version on leetcode is failing that test and returning true
instead. I have no clue why. My solution is by no means the best solution but I tried a handful of tests on my local and it seemed to get the job done. Any advice on how to fix this, or insight as to why I'm failing the test case on leetcode?
Initially I thought it was the way I structured my conditional when listLength is 2, but I changed that to something I'm sure would work and it didn't change the outcome.
edit: link to leetcode question https://leetcode.com/problems/palindrome-linked-list/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 @JoshuaWood 指出的,我的操作假设我的列表具有 JavaScript 数组本机的数组属性,但这是查看问题的错误方式。我必须使用他们提供给我的指针和数据来处理该列表。 (参见下面的定义)
一旦我意识到我将无法使用任何数组属性,而只能使用上面定义中给我的属性,我就能够重新格式化我的解决方案并提出这样:
基本上我可以导航到列表的末尾,存储末尾的内容,将其与开头进行比较,如果它们相等则弹出这些值。如果它们不相等,则该列表不是回文列表。如果是,那么我会迭代到列表中的下一个条目并从末尾弹出。这一过程一直持续到您到达只剩下 1 个或 2 个节点的列表,如果有两个则进行最终比较,或者如果只剩下一个节点则简单地返回 true,因为该节点将等于其自身。
希望这有帮助!一旦我意识到它们当然不会有数组属性,这一切就变得更有意义了。
As @JoshuaWood pointed out, I was operating under the assumption that my list had array properties native to JavaScript arrays, but this is an incorrect way of viewing the problem. I had to quite literally handle the list with the pointer and data they provided me. (see definition below)
Once I realized I wouldn't be able to use any array properties, and just use the properties given to me in the definition above, I was able to reformat my solution and come up with this:
Basically I can navigate to the end of the list, store what's at the end, compare it to the beginning, and pop off those values if they are equal. If they're not equal, then the list is not a Palindrome. If they are, then I iterate to the next entry in the list and pop off the end. This continues until you either reach a list with only 1 or 2 left, does the final comparison if there are two, or simply returns true if there's only one node left since that node will be equal to itself.
Hope this helps! Once I realized that of course they wouldn't have array properties, it all made a lot more sense.