作者benchen0812 (あBen)
看板Python
标题[问题] 想请问leetcode694和inner func相关问题
时间Wed Mar 27 12:52:48 2019
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/cn.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