-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gengkaiyang
committed
Jul 29, 2021
1 parent
bf6648e
commit c2cc5cd
Showing
6 changed files
with
1,764 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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对象流,对于子进程返回大量数据需要实时读取的情况更适合 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); |
Oops, something went wrong.