This article has been machine-translated from Chinese. The translation may contain inaccuracies or awkward phrasing. If in doubt, please refer to the original Chinese version.
- Multiple choice: 7 questions, 21 points
- Multiple select: 5 questions, 30 points
- Fill-in-the-blank: 2 questions, forgot how many points
- Short answer: 2 questions, don’t remember
- Programming: 3 problems at easy, easy, medium difficulty. Core code mode. Total code volume under 40 lines.
Finished the programming problems in 20 minutes, submitted, and went out to have fun. Tsk.
Short Answer 2 - Implementing a get Function
After the exam, I let Copilot auto-complete it. Hilarious.
let obj = { a: [{ b: { c: 1 } }] };
// console.log(obj.a[0].b.c)
function get(obj, str) {
str = str.split('.');
let len = str.length;
for (let i = 0; i < len; ++i) {
if (str[i] && str[i].indexOf('[') != -1) {
let index = str[i].match(/\[(\d+)\]/)[1];
let name = str[i].split('[')[0];
if (name in obj) {
obj = obj[name][index];
} else {
return undefined;
}
} else if (str[i] in obj && obj[str[i]]) {
obj = obj[str[i]];
} else {
return undefined;
}
}
return obj;
}
console.log(get(obj, 'a[0].b.c'));
console.log(get(obj, 'a[0].b.s'));
console.log(get(obj, 'a.s.q'));
Programming Problem 1 - Fibonacci
Fibonacci, one-liner:
return n == 0 || n == 1? n: fib(n-1)+fib(n-2)
Programming Problem 2 - Bracket Matching
Solved instantly. Just use a stack, ignore * characters.
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param str string字符串 输入字符串
* @return bool布尔型
*/
function isVerified(str) {
// write code here
let len = str.length;
let s = [];
for (let i = 0; i < len; ++i) {
if (str[i] == '*') continue;
if (str[i] == '{' || str[i] == '[' || str[i] == '(') {
s.push(str[i]);
} else {
let top = s[s.length - 1];
if ((str[i] == '}' && top == '{') || (str[i] == ']' && top == '[') || (str[i] == ')' && top == '(')) {
s.pop();
}
}
}
return s.length == 0;
}
module.exports = {
isVerified: isVerified,
};
Programming Problem 3 - Longest Increasing Subsequence Length
Done this so many times. Just DP.
dp[i]represents the length of the longest increasing subsequence from index0toi. Each time, find thejfrom0toi-1that can form an increasing sequence with the currenti, and take the maximum asdp[i].
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param arr int整型一维数组 数组
* @return int整型
*/
function lengthOfLis(arr) {
// write code here
let len = arr.length;
let dp = new Array(len).fill(1);
dp[0] = 1;
for (let i = 1; i < len; ++i) {
for (let j = 0; j < i; ++j) {
if (arr[i] > arr[j]) dp[i] = Math.max(dp[j] + 1, dp[i]);
}
}
return dp[len - 1];
}
module.exports = {
lengthOfLis: lengthOfLis,
};
// 求数组最长递增子序列长度
喜欢的话,留下你的评论吧~