有赞前端面经-一面
做题
var isValid = function(s) {
// code here
if(s.length <2) return false;
let stack = []; let obj = {')':'(', ']':'[', '}':'{'}; stack.push(s[0]); for(let i=1; i<s.length; i++) { if(stack.length == 0) { stack.push(s[i]); continue; } if(obj[s[i]] === stack[stack.length-1]) { stack.pop(); } else{ stack.push(s[i]) } } return stack.length === 0;
};
console.log('======有效的括号========');
console.log('should -> ', true);
console.log('result -> ', isValid('()'));
console.log('should -> ', true);
console.log('result -> ', isValid('()[]{}'));
console.log('should -> ', false);
console.log('result -> ', isValid('()[}'));
console.log('should -> ', false);
console.log('result -> ', isValid('(]'));
console.log('should -> ', true);
console.log('result -> ', isValid('([])'));
/**
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和
tips: 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。
示例输入: '197880986', '5112281222'
示例输出: '5310162208'
/
var addStrings = function (num1, num2) {
// code here
let i = num1.length;
let j = num2.length;if(i > j){
let arr = new Array(i - j).fill(0).join(''); num2 = arr + num2;
}
else if(i < j) {let arr = new Array(j - i).fill(0).join(''); num1 = arr + num1;
}
let res = []; let last = 0; while(i >= 0) { let curSum = Number(num1[i]) + Number(num2[i]) + last; if(curSum < 10) { res.unshift(curSum); last = 0; } else{ res.unshift(curSum - 10); last = 1; } i--; } return res.join('');
};
console.log('result --> ' + addStrings('197880986', '5112281222'));
console.log('want ----> ' + '5310162208');
console.log('[equal] -> ' + (addStrings('197880986', '5112281222') === '5310162208'));