Leetcode 解决方案适用于我的电脑,但不适用于网站(有效括号)

发布于 2025-01-20 09:50:18 字数 1611 浏览 1 评论 0原文

我对Leetcode是个新手,并且解决了这个问题的解决方案,但我没有看到任何明显的问题。当我在计算机上运行代码在问题上时,它在其上失败了,但是在网站上,它给出了错误的结果。

const leftBrackets = ["(", "{", "["];
const matchBrackets = { ")": "(", "}": "{", "]": "[" };
const testStack = [];

var isValid = function (s) {
  for (const char of s) {
    if (leftBrackets.includes(char)) {
      testStack.push(char);
    } else if (testStack[testStack.length - 1] === matchBrackets[char]) {
      testStack.pop();
    }
  }

  if (testStack.length === 0) return true;
  else return false;
};

这是一个问题: https://leetcode.com/problems/problems/valid-parenthises/submissions/submissions/submissions/submissions/submissions/

这是我对问题的回答。当我运行它时,网站表示代码作为输入的“ {[[]}}”的结果错误。它说它返回false,但是当我在计算机上的nodejs运行它时,它返回真实。有人知道为什么会发生这种情况吗?我只想能够验证何时解决问题的解决方案。如果您要回答,我也希望对我的解决方案进行反馈。我正在尝试使我的解决方案更加有效,而不必求助于所有内容。 O(n)似乎可以很容易地进行。

感谢任何回答的人!

编辑:我在下面的一个问题中添加了完整的解决方案,有人在此问题上看到了这篇文章。它在速度方面做得很好(56毫秒,比98%快),所以我认为它非常有效。这似乎也是随机的大声笑。

var isValid = function (s) {
  
  const leftBrackets = ["(", "{", "["];
  const matchBrackets = { ")": "(", "}": "{", "]": "[" };
  const testStack = [];
  
  if (s.length % 2 !== 0) return false;
  
  for (const char of s) {
    if (leftBrackets.includes(char)) {
      testStack.push(char);
    } else if (testStack[testStack.length - 1] === matchBrackets[char]) {
      testStack.pop();
    } else return false;
  }
  
  if (testStack.length === 0) return true;
  else return false;
};

I'm fairly new to leetcode and am getting a wrong result for my solution to this problem but I don't see any obvious problems with it. When I run the code on my computer on the question it is failing on it works but on the site it gives the wrong result.

const leftBrackets = ["(", "{", "["];
const matchBrackets = { ")": "(", "}": "{", "]": "[" };
const testStack = [];

var isValid = function (s) {
  for (const char of s) {
    if (leftBrackets.includes(char)) {
      testStack.push(char);
    } else if (testStack[testStack.length - 1] === matchBrackets[char]) {
      testStack.pop();
    }
  }

  if (testStack.length === 0) return true;
  else return false;
};

This is the question: https://leetcode.com/problems/valid-parentheses/submissions/

This is my answer to the question. When I run it the site says the code got the wrong result for "{[]}" as the input. It says it returns false, but when I run it in NodeJS on my computer it returns true. Does anyone know why this happens? I just want to be able to verify when I have got the solution to a problem. I'd also like feedback on my solution if you are answering anyway. I'm trying to make my solutions more efficient without resorting to just looking everything up. This seems like it should be possible easily with O(n).

Thanks to anyone that answers!

EDIT: I added my full solution below in the off-chance someone working on this question sees this post. It did very well on the speed thing (56ms, faster than 98%) so I think its pretty efficient. It also seems to be random how long it takes lol.

var isValid = function (s) {
  
  const leftBrackets = ["(", "{", "["];
  const matchBrackets = { ")": "(", "}": "{", "]": "[" };
  const testStack = [];
  
  if (s.length % 2 !== 0) return false;
  
  for (const char of s) {
    if (leftBrackets.includes(char)) {
      testStack.push(char);
    } else if (testStack[testStack.length - 1] === matchBrackets[char]) {
      testStack.pop();
    } else return false;
  }
  
  if (testStack.length === 0) return true;
  else return false;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小清晰的声音 2025-01-27 09:50:18

您可能只需要将testAck移动到该功能中,否则它将持续使用iSvalid的所有调用。这是另一个错误的负面示例:

const leftBrackets = ["(", "{", "["];
const matchBrackets = { ")": "(", "}": "{", "]": "[" };
const testStack = [];

var isValid = function (s) {
  for (const char of s) {
    if (leftBrackets.includes(char)) {
      testStack.push(char);
    } else if (testStack[testStack.length - 1] === matchBrackets[char]) {
      testStack.pop();
    }
  }

  if (testStack.length === 0) return true;
  else return false;
};

console.log(isValid('('));
console.log(isValid('[]'));

因此

var isValid = function (s) {
  const testStack = [];

,每个调用都有自己的数组。

您的问题不包括问题描述,但潜在的错误是您没有检查是否使用了错误的正确括号。例如,如果您希望字符串

(])

失败,则需要更改逻辑 - 按下或pop,如果不满足条件,也许您要返回false

You probably just need to move the testStack inside the function, otherwise it'll persist for all calls of isValid. Here's another false negative example:

const leftBrackets = ["(", "{", "["];
const matchBrackets = { ")": "(", "}": "{", "]": "[" };
const testStack = [];

var isValid = function (s) {
  for (const char of s) {
    if (leftBrackets.includes(char)) {
      testStack.push(char);
    } else if (testStack[testStack.length - 1] === matchBrackets[char]) {
      testStack.pop();
    }
  }

  if (testStack.length === 0) return true;
  else return false;
};

console.log(isValid('('));
console.log(isValid('[]'));

So just do

var isValid = function (s) {
  const testStack = [];

so that each invocation has its own array.

Your question doesn't include the problem description, but a potential bug is that you're not checking whether the wrong right bracket is used. For example, if you want the string

(])

to fail, you'll need to change your logic - either push or pop, and if neither condition is satisfied, perhaps you want to return false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文