注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
1. 如果你觉得你付出努力,都没啥效果。
1. 我建议你看看这个: https://www.zhihu.com/question/24051063
2. 大部份人是慢思维,至少我是
4. 现在要用几天的时间刷一道题
5. 我看别人都叫我一道题刷几十遍,就是记不住才刷几十遍。就是说刷一次的功效极低, 才要重复?
6. 我觉得每一行代码都ask why. 问得越深越好,你一次就记住了. 你为什么看答案的时候要ask why, 因为你自己写的时候就像dfs, 会有n步骤, 你会开始confuse, swing, 也不知道写什么,为什么要写?
7. so you have to deep thinking for each line of code
e.g. https://leetcode.com/problems/minesweeper/- /**
- * @param {character[][]} board
- * @param {number[]} click
- * @return {character[][]}
- */
- var dfs = function(d, b, click) {
- // * why the cell in board is the underneath actual value? <----
- // * because if we know the actual value, we can display the value in the board for user
-
- // * why click has row and col? <---
- // * because it is the start position in the grid
- const r = click[0];
- const c = click[1];
- // * this is row, go down
- const m = b.length;
- // * this is col, go right
- const n = b[0].length;
-
- let mNum = 0;
- // * why the dfs queue arr is empty at each dfs level? <---
- // * because next dfs level, the click will have its own dfs queue
- // * because 8 directions, cause it own queue, in own dfs level
- const empty = [];
-
- // * why do we hit the directions straight away, there is no loop to travel? <---
- // * yes, because there is no loop here, so only direction
-
- // * if we click a cell, the direction is like a sum with diff direciton of shine, why? <---
- // * because the middle is the central cell, then 8 directions are the shines
-
- // * why we loop the 8 directions? <---
- // * because click 1 spot, we will hit all 8 directions
- // * it can be 4 directions or an upgrade: 8 directions
- for(let [r1, c1] of d) {
- // * why we need row = curr_row + row_d, col = curr_col + col_d? <---
- // * because after loop each direction, we have a new move
- // * we can check boundary with this new move
- const r2 = r + r1;
- const c2 = c + c1;
-
- // * so this is to check the future boundary
- if(r2 < 0 || c2 < 0 || r2 >= m || c2 >= n) continue;
-
- // * why we only check boundary, we don't have other condi to check? <--
- // * because after boundary check pass, we will hit mine (M, acc), E (empty, queue), B (blank, seen),
- // * D (digit, seen), X (reveal, never)
- // * why we don't need seen here? <---
- // * because there is no cycle;
- // * seen good for cycle
-
- // * why we need to care mine (M = mine)? <---
- // * because if we acc all 8 directions, we know the total mine number, so we can assign to start point????????
- // * why we need to care empty square (E = empty)? <---
- // * because if we see empty (E), then we can push to arr, then loop this arr -> dfs
- // * this allow new start point for dfs
- // * why we don't care revealved square (B = blank)? <--
- // * because already know before
- // * not contribute to acc mine or dfs queue
- // * why we don't care digit? (D = digit)? <--
- // * because already know before
- // * not contribute to acc mine or dfs queue
- // * why we don't care revealed mine (X = explode)? <--
- // * I don't think we will see this, if we see explode, then it is game over
- // * the game still going so no care revealed.
-
- if(b[r2][c2] === 'M') {
- // * why acc mine num? <---
- // * because we need to asssgin to start point
- // * why we see a future mine here, we stop, we do not do dfs? <----
- // * because if we see a mine, it means this path cannot go further, so need to stop dfs path!!!!!!!!!
- ++mNum;
- } else if(b[r2][c2] === 'E') {
- // * if we can see empty, this means dfs path can continue, can keep going <---
- // * so why put start point to arr? <---
- // * because those dfs path can go down
- empty.push([r2, c2]);
- }
- } // el
-
- // * if we see mine, we need to stop directly, even we have some dfs queue?
- // * because ...?
- if(mNum > 0) {
- b[r][c] = String(mNum);
- } else {
- // * so why we revreal the click? <---
- // * because in this dfs level, this click is used, so reveal it
- // * the dfs queue is below, is acting like that it will simulate the click further (further reveal)
- b[r][c] = 'B';
-
- // * why the element in queue order is the dir order? <----
- // * because we loop direction, then add to dfs queue, so dfs queue order === direction order
- for(let [x, y] of empty) {
- dfs(d, b, [x, y]);
- }
- }
- }
- var updateBoard = function(b, click) {
- // * why do we need the 8 cell grid direction? <---
- // * because click 1 spot, then all 8 directions get exploded.
- const d = [[-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1]];
- // * why click has row?
- // * because it will land on grid
- const r = click[0];
- // * why click has col?
- // * because it will land on the grid
- const c = click[1];
-
- // * why there is no loop then dfs? <---
- // * because we have a click (start_row, start_col), then we just dfs init, goes down
-
- // * why we need to handle click the mine directly? <---
- // * because if we don't handle here now, when goes into dfs, we only look at future 8 directions.
- // * the curr one will be ignored, so we need to handle now.
- // * why M === untouched mine? <----
- // * because M is the init letter of Mine; it is a good and untouched mine.
- // * why X === revealed mine? <----
- // * because X looks like exploding and cross means bad
- if(b[r][c] === 'M') {
- b[r][c] = 'X';
- // * why we need to return the board? <---
- // * because we click on the mine, then explode, need to return board
- return b;
- } else {
- // * why pass direction, beacuse new_row = row + row_d? <---
- // * new_col = col + col_d
- // * so we need the next move
- dfs(d, b, click);
- return b;
- }
- };
复制代码 |