一、手写算法
https://leetcode-cn.com/problems/decode-string/
思路
-
代码
/**
* @param {string} s
* @return {string}
*/
var decodeString = function(s) {
let result = '',
numStack = [],
strStack = [],
num = 0;
for (const char of s) {
if (!isNaN(char)) {
num = num * 10 + Number(char);
} else if (char === '[') {
strStack.push(result);
result = '';
numStack.push(num);
num = 0;
} else if (char === ']') {
let nu = numStack.pop();
result = strStack.pop() + result.repeat(nu);
} else {
result += char;
}
}
return result;
};
复杂度分析
时间复杂度:O(n)
- 空间复杂度:O(n)
二、编程题
// 1.手写题:https://bigfrontend.dev/zh/problem/implement-Object.is
function is(a,b) {
// SameValue algorithm
if (a === b) { // Steps 1-5, 7-10
// Steps 6.b-6.e: +0 != -0
return a !== 0 || 1 / a === 1 / b;
} else {
// Step 6.a: NaN == NaN
return a !== a && b !== b;
}
}