conardli / awesome-coding-js Goto Github PK
View Code? Open in Web Editor NEW用JavaScript实现的算法和数据结构,附详细解释和刷题指南
用JavaScript实现的算法和数据结构,附详细解释和刷题指南
这个方法不能区分1和’1‘,建议换成Map,或者key上面拼一个类型上去。
https://www.conardli.top/docs/JavaScript/%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F.html
开头的 ”在合适的时候才创建对像“ 应该是 对象 而不是对像
let arr = [2, 3, 11, 4]
let r = arr.sort((a, b) => {
return b % 2 - a % 2
})
console.log(r)
call的代码实现写的有漏洞,应该改为如下代码
function fn1() {
console.log(this, 1);
}
Function.prototype.myCall = function (context = window, ...args) {
if (this === Function.prototype) {
return undefined; // 用于防止 Function.prototype.myCall() 直接调用
}
context = Object(context);
const fn = Symbol();
context[fn] = this;
const result = contextfn;
delete context[fn];
return result;
}
fn1.myCall('hello')
快速排序里面的第二种写法代码里面有一处有错误。
第二行这里的
if (end - start < 2) { return; }
if里面应该是“end - start < 1”,不然的话当刚好有两个元素的话就无法去判断排序了。
比如这个数组“[23, 11, 5, 65, 7, 33, 2, 26]”,如果按照你的写法的话会输出“[ 2, 7, 5, 11, 23, 26, 33, 65 ]”,如果改成我那样的话就会正确输出“[ 2, 5, 7, 11, 23, 26, 33, 65 ]”
当前方法实现输出不符合预期,可以用substring拼接改一下
第一层循环长度应该是length-1吧
// 4.挂载回调函数
window[cbFuncName] = function (data) {
callback(data);
// 处理完回调函数的数据之后,删除jsonp的script标签
document.body.removeChild(scriptEle);
delete window[cbFuncName]
}
function Permutation(str) {
const queue = str.split('');
queue.sort();
return PermutationCore(queue);
}
function PermutationCore(queue) {
let result = [];
if (queue.length === 1) {
return queue;
}
queue.forEach((item, index) => {
const newArr = queue.slice(0);
newArr.splice(index, 1);
const newArray = PermutationCore(newArr).map(i => (item + i));
result = result.concat(newArray);
});
return result;
}
console.log(Permutation('abc')); // ["abc", "acb", "bac", "bca", "cab", "cba"]
arr.slice();
arr.concat();
文中提到这两个方法是浅拷贝,应该是深拷贝的
let arr1 = [1,2,3,4];
let arr2 = arr1.slice(0);
let arr3 = arr1.concat();
arr2[1] =6;
arr3[1] = 6;
console.log(arr1); // [1,2,3,4]
console.log(arr2); // [1,6,3,4]
console.log(arr3); // [1,6,3,4]
我感觉js并不是一个有序的数据结构,而且你用for in循环并不能保证‘第一次出现’这个条件
var inorderTraversal = function (root, array = []) {
if (root) {
inorderTraversal(root.left, array);
array.push(root.val);
inorderTraversal(root.right, array);
}
return array;
};
console.log(inorderTraversal([1,null,2,3]))
打印[undefined]
您好,感觉字符串-正则表达式那道题的题目里第二个符号应该是'*',而不应该是''吧?
递归算法:好像写错了,解法思路是对的,但是运行就报错,
运行环境:vscode集成leetcode插件
/*
* @lc app=leetcode.cn id=230 lang=javascript
*
* [230] 二叉搜索树中第K小的元素
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} k
* @return {number}
*/
function kthSmallest(root, k) {
const arr = [];
loopThrough(root, arr);
if (k > 0 && k <= arr.length) {
return arr[k - 1];
}
return null;
}
function loopThrough(node, arr) {
if (node) {
loopThrough(node.left, arr);
arr.push(node);
loopThrough(node.right, arr);
}
}
✘ Wrong Answer
✘ 0/91 cases passed (N/A)
✘ testcase: '[3,1,4,null,2]\n1'
✘ answer: NaN
✘ expected_answer: 1
✘ stdout:
pre = array[i]; 应该是 pre = i
function FindPathCore(node, expectNumber, stack, sum, result) {
stack.push(node.val);
sum += node.val;
if (!node.left && !node.right && sum === expectNumber) {
result.push(stack.slice(0));
}
if (node.left) {
FindPathCore(node.left, expectNumber, stack, sum, result);
}
if (node.right) {
FindPathCore(node.right, expectNumber, stack, sum, result);
}
stack.pop();
}
当stack[stack.length-1]存在并且没访问过时,stack.pop()会将右子树切断,造成结果项缺失。
选择排序的稳定性应该是 不稳定 的哈
container[i] >= 0时,container[i]对应字符在字符串中的下标,所以实际使用时 minIndex = [传入的字符串长度或者无穷大]。
哈哈哈,当然这里不涉及具体问题,思路正确。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.