Re: [閒聊] 每日LeetCode已回收

看板Marginalman作者 (みけねこ的鼻屎)時間3年前 (2022/11/01 14:13), 編輯推噓4(400)
留言4則, 4人參與, 3年前最新討論串78/719 (看更多)
1706. Where Will the Ball Fall 給予一個二維陣列表示 2D 的 Ball Fall 遊戲,每格意義如下: 1:\ 往右 -1:/ 往左 https://assets.leetcode.com/uploads/2019/09/26/ball.jpg
求出從每個位置的頂端放入一顆球最後該球會從哪個出口掉出來 如果球卡死沒掉出來返回-1。 Example: Input: grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]] Output: [1,-1,-1,-1,-1] Explanation: This example is shown in the photo. Ball b0 is dropped at column 0 and falls out of the box at column 1. Ball b1 is dropped at column 1 and will get stuck in the box between column 2 and 3 and row 1. Ball b2 is dropped at column 2 and will get stuck on the box between column 2 and 3 and row 0. Ball b3 is dropped at column 3 and will get stuck on the box between column 2 and 3 and row 0. Ball b4 is dropped at column 4 and will get stuck on the box between column 2 and 3 and row 1. 思路: 1.起點在箱子頂端(第一列的每一行),判斷放入時兩邊的方向是否一致,必須是 \ \ 或 / / 球才可以到下一格,若是右邊的四種情況 \/ \/ \|(撞牆) |/ 球才會卡死。 2.方向為右就檢查右邊的斜坡方向,方向為左就檢查左邊的,如果檢查可以到下一格 就依照\ \ 或/ / 來決定球往下移動的時候是往左還往右。 3.最後檢查y是否等於R,如果相等就表示他是從出口出來的,球的離開位置為x,否則 球為卡死設為 -1 Java Code: class Solution { public int[] findBall(int[][] grid) { int R = grid.length, C = grid[0].length; int[] res = new int[C]; for(int i = 0; i < C; i++) { int x = i, y = 0;; while (x < C && y < R) { if(grid[y][x] == 1) { if(x + 1 >= C || grid[y][x + 1] != 1) break; x++; y++; } else { if(x - 1 < 0 || grid[y][x - 1] != -1) break; x--; y++; } } res[i] = (y == R) ? x : -1; } return res; } } 我現在情緒起伏比較大一點 剛剛越思考越害怕開始哭就發文了 現在好像又好一點了 有人說看能不能看其他東西好一點,有啊 我最近在刷LeetCode的時候心情會好一點 我也不知道是怎麼樣了欸 -- https://i.imgur.com/YPBHGGE.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.160.72.244 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1667283229.A.D93.html

11/01 14:15, 3年前 , 1F
大師
11/01 14:15, 1F

11/01 14:17, 3年前 , 2F
笑了 這心得
11/01 14:17, 2F

11/01 14:23, 3年前 , 3F
靠北 最後笑了
11/01 14:23, 3F

11/01 14:33, 3年前 , 4F
靠背 又是複製
11/01 14:33, 4F
文章代碼(AID): #1ZOBaTsJ (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1ZOBaTsJ (Marginalman)