Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
gengkaiyang committed Jul 29, 2021
1 parent bf6648e commit c2cc5cd
Show file tree
Hide file tree
Showing 6 changed files with 1,764 additions and 129 deletions.
76 changes: 76 additions & 0 deletions leetcode/46.全排列.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* @lc app=leetcode.cn id=46 lang=javascript
*
* [46] 全排列
*
* https://leetcode-cn.com/problems/permutations/description/
*
* algorithms
* Medium (78.10%)
* Likes: 1466
* Dislikes: 0
* Total Accepted: 369.3K
* Total Submissions: 472.7K
* Testcase Example: '[1,2,3]'
*
* 给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
*
*
*
* 示例 1:
*
*
* 输入:nums = [1,2,3]
* 输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
*
*
* 示例 2:
*
*
* 输入:nums = [0,1]
* 输出:[[0,1],[1,0]]
*
*
* 示例 3:
*
*
* 输入:nums = [1]
* 输出:[[1]]
*
*
*
*
* 提示:
*
*
* 1
* -10
* nums 中的所有整数 互不相同
*
*
*/

// @lc code=start
/**
* @param {number[]} nums
* @return {number[][]}
*/
var permute = function(nums) {
const stack = [];
const len = nums.length;
stack.push([nums.shift()]);
while(nums.length) {
let n = nums.shift();
let l = stack.length;
for(let i = 0; i < l; i++) {
stack.push(stack[i].concat(n));
stack.push([n].concat(stack[i]));
}
stack.push([n]);
}
return stack.filter(num => num.length === len);
}
// @lc code=end

console.log(permute([1, 2, 3]))

18 changes: 18 additions & 0 deletions node-process/child-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const childProcess = require('child_process');

const spawn = childProcess.spawn;
const exec = childProcess.exec;
const execFile = childProcess.execFile;
const fork = childProcess.fork;

// exec执行会产生一个子shell,并且可以处理所有的shell命令,一般情况下我们在需要利用shell功能的时候才会用到
exec( 'for i in $( ls -LR ); do echo item: $i; done', ( e, stdout, stderr)=> {
if ( e instanceof Error) {
console.error( e);
throw e;
}
console.log( 'stdout ', stdout);
console.log( 'stderr ', stderr);
});

// spawn创建子进程会返回一个ChildProcess对象流,对于子进程返回大量数据需要实时读取的情况更适合
7 changes: 7 additions & 0 deletions node-process/children.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

process.on('message', data => {
console.log('child receive message:%s from parent process', data);
process.exit(0);
});

process.send({ message: 'hello child, this is child process' });
16 changes: 16 additions & 0 deletions node-process/parent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const fork = require('child_process').fork;
const path = require('path');

const child = fork('./node-process/children.js');
/**
* fork是spawn的一个特殊情况,其返回一个childProcess
* 并且在fork一个子进程时,会创建一个IPC通道,允许父进程与子进程之间进行通信
* * 在子进程中,我们可以通过process.on('message')和process.send()事件和方法与父进程通信
* * 在父进程中,我们可以通过childProcess.on('message')和childProcess.send()事件和方法与子进程通信
*/
child.on('message', data => {
console.log('parent receive message:%s from child process', data);
process.exit(0);
});

child.send({ message: 'hello child, this is parent process' });
Loading

0 comments on commit c2cc5cd

Please sign in to comment.