Do not Tucao cattle net bug, the online written experience is very poor. At that time, it seriously affected the mood, psychological quality or to improve, no matter when, programming to maintain a peaceful state of mind.
Let's talk about the solution directly. There are four questions in total. The first one is very simple. The second and third questions give the solution. The fourth one is too late to be done. I haven't seen any of them.
Second questions
If there are several groups of 1 and there are 1 in the eight squares around 1, it is regarded as the same group.
I started to traverse from the top left corner, so I only need to recurse five directions in eight directions. That is, right top, right, right bottom, positive bottom, left bottom. When using a flag array to store records, the traversed ones will not be traversed.
let arr = [[1, 0, 1, 0], [1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 1, 0]]; getOne(arr); function getOne(arr) { let row = arr.length; let col = arr[0].length; let flag = []; let sum = 0; for (let i = 0; i < row; i++) { flag.push([]); } for (let i = 0; i < row; i++) { for (let j = 0; j < col; j++) { if (!flag[i][j]) { isOne(i, j, true); } } } return sum; function isOne(i, j, origin) { if (i < row && i >= 0 && j < col && j >= 0) { if (!flag[i][j]) { flag[i][j] = true; if (arr[i][j] === 1) { isOne(i + 1, j, false); isOne(i, j + 1, false); isOne(i + 1, j + 1, false); isOne(i + 1, j - 1, false); isOne(i - 1, j + 1, false); if (origin) { console.log(i, j); sum++; } } } } } }
Third questions
n%xxxx ා,% ා, the delimiter, requires that xxxx in it be rewritten n times. Nesting is allowed.
function trans(line) { if (line.indexOf('#') === -1) { return line; } let newLine = []; let times_arr = []; let times = 0; let temp = []; let flag = false; let headnum = 0; while (line.length !== 0) { let item = line.shift(); if (!flag) { if (item === '%') { headnum++; flag = true; while (newLine[newLine.length - 1] >= '0' && newLine[newLine.length - 1] <= '9') { times_arr.unshift(newLine.pop()); } times = parseInt(times_arr.join('')); times_arr = []; } else if (item >= 'a' && item <= 'z' || item >= '0' && item <= '9') { newLine.push(item); } } else { if (item === '%') { headnum++; temp.push(item); } else if (item === '#') { headnum--; if (headnum === 0) { flag = false; for (let i = 0; i < times; i++) { newLine = newLine.concat(temp); } temp = []; } else { temp.push(item); } } else { temp.push(item); } } } return trans(newLine); } var line = '2a2%a10%f##'.split(''); trans(line); function tran(line) { let last = line.lastIndexOf('%'); while(last !== -1) { for (let i = last + 1; i < line.length; i++) { if(line[i] === '#') { let substr = line.slice(last + 1, i); let times_start; for(let i = last - 1; i >= 0; i--) { if(line[i] < '0' || line[i] > '9') { times_start = i + 1; break; } } let line_left = line.slice(0, times_start); let line_right = line.slice(i + 1); let times = parseInt(line.slice(times_start, last).join('')); for(let i = 0; i < times; i++) { line_left = line_left.concat(substr); } line = line_left.concat(line_right); console.log(line.join('')); last = line.lastIndexOf('%'); break; } } } return line.join(''); }
trans function is recursive method, tran function is iterative method. It turns out that this problem is more convenient to use iteration. More than 20 lines of code.
Recursion method: read% ා and save the characters in it, and double them directly. Until there is no more Chen or%.
Iteration method: start from the last% and continue to the left. Until there is no%.
Save time, no error handling.