Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

类似逆波兰表达式的算法题 #73

Open
Liqiuyue9597 opened this issue Dec 2, 2020 · 1 comment
Open

类似逆波兰表达式的算法题 #73

Liqiuyue9597 opened this issue Dec 2, 2020 · 1 comment

Comments

@Liqiuyue9597
Copy link
Owner

第一次在面试中碰到出笔试题形式的算法题。上来就是一大段文字,什么小Q小强怎么的,然后看下来大概就是:
将字符串HG[3|B[2|AC]]F按照括号里的数量展开 --> HG[3|BACAC]F --> HGBACACBACACBACACF

@Liqiuyue9597
Copy link
Owner Author

function test(str) {
  const alp = [];
  let temp = [];
  for (let i = 0; i < str.length; i++) {
    if (str[i] !== "[" && str[i] !== "]") {
      alp.push(str[i]);
    } else if (str[i] === "]") {
      while (alp[alp.length - 1] !== "|") {
        temp.push(alp.pop());
      }
      alp.pop(); //将符号|去掉
      const time = Number(alp.pop());//拿到数字
      temp = temp.reverse().join("").repeat(time);
      alp.push(temp);
      temp = [];
    }
  }
  const res = alp.join("");
  return res;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant