Writing algorithm problems with Nodejs
We use the algorithm at the front end of the place is not many, but for the school recruit written test, have to solve the algorithm problem to practice!
Finally determined to conquer the algorithm problem. It is not possible to find JS to build the input and output stream as the C language itself. Can only go back to learn C language? Actually not, node can also help us to complete! and the written test support with Nodejs, in fact, is to use JS programming, just use the node of some input and output stream method.
Let's look at the simplest use of templates: (reprint)
var readline = require(‘readline‘);rl = readline.createInterface({ input: process.stdin, output: process.stdout});rl.on(‘line‘, function(data) { // 获取输入 var inputs = data.trim().split(‘ ‘); // 处理 var result = deal(inputs); // 输出结果 console.log(result);});function deal(inputs) { var result = ‘‘; // dosomething return result;}
This simple set of templates will solve the general algorithm problem! Finally put up a front end algorithm problem Example:
Title: Enter n (n<=10000) numbers to find the maximum and minimum values in these n numbers. The absolute value of each number is not greater than 1000000.
var readline = require(‘readline‘);var rl = readline.createInterface({ input:process.stdin, output:process.stdout,});var num = 0; rl.on(‘line‘,function(input){ if(num==0){ num = input.trim(); } else{ var iptArr = input.split(‘ ‘); if(iptArr.length==num){ var maxNum = Math.max.apply(null,iptArr); var minNum = Math.min.apply(null,iptArr); var result = maxNum + ‘ ‘ + minNum; console.log(result); return result; } }})//在终端输入node max_min //文件名5 //输入N个数的数量12 18 5 20 10 //N个数20 5 //返回结果
For more ways to use node in algorithmic questions, see: https://www.cnblogs.com/floor/p/6667059.html
Front-end interview common algorithm questions see this: http://web.jobbole.com/88471/
How does the front end deal with the written algorithm? (programming with node)