一、手写算法

https://leetcode-cn.com/problems/decode-string/

思路


  • 代码

    1. /**
    2. * @param {string} s
    3. * @return {string}
    4. */
    5. var decodeString = function(s) {
    6. let result = '',
    7. numStack = [],
    8. strStack = [],
    9. num = 0;
    10. for (const char of s) {
    11. if (!isNaN(char)) {
    12. num = num * 10 + Number(char);
    13. } else if (char === '[') {
    14. strStack.push(result);
    15. result = '';
    16. numStack.push(num);
    17. num = 0;
    18. } else if (char === ']') {
    19. let nu = numStack.pop();
    20. result = strStack.pop() + result.repeat(nu);
    21. } else {
    22. result += char;
    23. }
    24. }
    25. return result;
    26. };

    复杂度分析

  • 时间复杂度:O(n)

  • 空间复杂度:O(n)

二、编程题

  1. // 1.手写题:https://bigfrontend.dev/zh/problem/implement-Object.is
  1. function is(a,b) {
  2. // SameValue algorithm
  3. if (a === b) { // Steps 1-5, 7-10
  4. // Steps 6.b-6.e: +0 != -0
  5. return a !== 0 || 1 / a === 1 / b;
  6. } else {
  7. // Step 6.a: NaN == NaN
  8. return a !== a && b !== b;
  9. }
  10. }