Python 板


LINE

Hi all, 今天刷到一題 leetcode694 題目是 Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Count the number of distinct islands. An island is considered to be the same as another if and only if one island can be translated (and not rotated or reflected) to equal the other. 先附上我的寫法還有解答 https://imgur.com/fnrJUsG 然後error message https://imgur.com/ycaClM6 這邊想請問的是 我看我的寫法和解答的寫法除了他用inner 然後我的function 是define 在class 底下之外 應該是沒有什麼太大差別 (如果有錯誤請幫我指證謝謝) 這邊想請問的是 如果我是用inner function 做recursion 請問function return address是不是也是存在stack 如果是的話 想請問一下為什麼我的寫法會max recursion depth exceeded? 但是他的寫法卻可以過? 兩種recursion depth 應該要一樣不是? 謝謝! --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 174.55.55.170
※ 文章網址: https://webptt.com/m.aspx?n=bbs/Python/M.1553662373.A.46E.html
1F:推 lemon651: 你有發現你跟解答的終止條件不一樣嗎?第一長寬應該是< 03/27 17:38
2F:→ lemon651: 不是<= 不過這不影響max depth,第二你把grid[x][y]看過 03/27 17:38
3F:→ lemon651: 的沒有紀錄起來也沒有轉成能當False的東西 等於你的每一 03/27 17:38
4F:→ lemon651: 層都會互相調用,肯定爆棧,舉個例你11 從左邊的往右邊d 03/27 17:38
5F:→ lemon651: fs 右邊的又往左dfs 等於這個遞歸完全不會終止 03/27 17:38
6F:推 lemon651: 你給的解答 用了seen紀錄過走過的點 並用一個set紀錄形 03/27 17:42
7F:→ lemon651: 狀 這題因為空間複雜度最差還是m * n所以用一個set紀錄 03/27 17:42
8F:→ lemon651: 走過的點不影響空間複雜度 03/27 17:43
阿我發現我是要讓 grid[x][y] = 0... 感謝! 沒想到犯了這麼蠢的問題ORZ ※ 編輯: benchen0812 (174.55.55.170), 03/27/2019 22:35:11
9F:推 lemon651: 我是建議你不要讓他等於0啦 這樣你走完一遍就把整個inpu 03/28 12:40
10F:→ lemon651: t改變了 而且還不可逆 03/28 12:40







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:iOS站內搜尋

TOP