
数组运算
// initializeconst arr = [];const arr = new array(size).fill(0); // [0,0,0,0,0]const arr = array.from({length: n}, (_, i) => i); // [0,1,2,...,n-1]// basic operationsarr.push(element); // add to endarr.pop(); // remove from endarr.unshift(element); // add to startarr.shift(); // remove from start// slicing and splicingarr.slice(startidx, endidx); // returns new array, endidx not includedarr.splice(startidx, deletecount, ...itemstoadd);// common methodsarr.map(x => x * 2); // returns new arrayarr.filter(x => x > 0); // returns new arrayarr.reduce((acc, curr) => acc + curr, initialvalue);arr.sort((a, b) => a - b); // ascendingarr.reverse();arr.join(''); // convert to stringarr.includes(element); // check existencearr.indexof(element); // first occurrencearr.lastindexof(element); // last occurrence
字符串操作
// creation and accessconst str = "hello";str.length;str[0] or str.charat(0);// common methodsstr.substring(startidx, endidx); // endidx not includedstr.substr(startidx, length); // deprecated but good to knowstr.slice(startidx, endidx); // can use negative indicesstr.split(''); // convert to arraystr.tolowercase();str.touppercase();str.trim(); // remove whitespacestr.replace(old, new);str.replaceall(old, new);str.startswith(prefix);str.endswith(suffix);str.includes(substr);str.repeat(count);
地图和设置
// mapconst map = new map();map.set(key, value);map.get(key);map.has(key);map.delete(key);map.clear();map.size;// setconst set = new set();set.add(value);set.has(value);set.delete(value);set.clear();set.size;// object as hashmapconst obj = {};obj[key] = value;key in obj; // check existencedelete obj[key];object.keys(obj);object.values(obj);object.entries(obj);
类和对象
class node { constructor(val) { this.val = val; this.next = null; }}// quick object creationconst obj = { key1: value1, key2: value2 };
通用数据结构
// queue using arrayconst queue = [];queue.push(element); // enqueuequeue.shift(); // dequeue// stack using arrayconst stack = [];stack.push(element);stack.pop();// linkedlist nodeclass listnode { constructor(val = 0, next = null) { this.val = val; this.next = next; }}// binary tree nodeclass treenode { constructor(val = 0, left = null, right = null) { this.val = val; this.left = left; this.right = right; }}// trie nodeclass trienode { constructor() { this.children = new map(); this.isendofword = false; }}
位操作
// common operationsn <> 1; // divide by 2n & 1; // check if oddn & (n-1); // remove last set bitn & -n; // get last set bitn | (1 << pos); // set bit at positionn & ~(1 << pos); // clear bit at positionn ^ (1 << pos); // toggle bit at position
常见模式和实用程序
// number operationsmath.max(...arr);math.min(...arr);math.floor(n);math.ceil(n);math.abs(n);number.max_safe_integer;number.min_safe_integer;infinity;-infinity;// random numbermath.random(); // [0, 1)math.floor(math.random() * n); // [0, n-1]// character code'a'.charcodeat(0); // 97string.fromcharcode(97); // 'a'// check typenumber.isinteger(n);array.isarray(arr);typeof variable;// parsingparseint(str);parsefloat(str);
常见的面试模式
// Two Pointerslet left = 0, right = arr.length - 1;while (left < right) { // process left++; right--;}// Sliding Windowlet left = 0;for (let right = 0; right < arr.length; right++) { // add arr[right] to window while (/* window condition */) { // remove arr[left] from window left++; }}// Binary Searchlet left = 0, right = arr.length - 1;while (left <= right) { const mid = Math.floor((left + right) / 2); if (arr[mid] === target) return mid; if (arr[mid] < target) left = mid + 1; else right = mid - 1;}
以上就是JavaScript 面试备忘单 – 第 1 部分的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1499351.html
微信扫一扫
支付宝扫一扫