{"id":2531,"date":"2022-03-27T22:19:13","date_gmt":"2022-03-27T14:19:13","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2531"},"modified":"2022-03-27T23:21:36","modified_gmt":"2022-03-27T15:21:36","slug":"%e7%ae%97%e6%b3%95%e5%85%a5%e9%97%a8%e7%ac%ac%e4%b8%83%e5%a4%a9","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2531","title":{"rendered":"\u7b97\u6cd5\u5165\u95e8\u7b2c\u4e03\u5929"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/study-plan\/algorithms\/?progress=7ob9vsg\">\u300c\u7b97\u6cd5\u300d &#8211; \u5b66\u4e60\u8ba1\u5212 &#8211; \u529b\u6263\uff08LeetCode\uff09\u5168\u7403\u6781\u5ba2\u631a\u7231\u7684\u6280\u672f\u6210\u957f\u5e73\u53f0 (leetcode-cn.com)<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 \/ \u6df1\u5ea6\u4f18\u5148\u641c\u7d22<\/h2>\n\n\n\n<p>\u7b2c\u4e00\u9898\u6df1\u5ea6\u4f18\u5148\u7684\uff0c\u7b80\u5355\u9898\uff0c\u9000\u51fa\u6761\u4ef6\u662f\u5173\u952e\uff0c\u4e0d\u8fc7\u4ed6\u7684\u9898\u76ee\u8bf4\u7684\u6bd4\u8f83\u96be\u7406\u89e3\u3002\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/**\n *\n 733. \u56fe\u50cf\u6e32\u67d3\n \u6709\u4e00\u5e45\u4ee5 m x n \u7684\u4e8c\u7ef4\u6574\u6570\u6570\u7ec4\u8868\u793a\u7684\u56fe\u753b image \uff0c\u5176\u4e2d image[i][j] \u8868\u793a\u8be5\u56fe\u753b\u7684\u50cf\u7d20\u503c\u5927\u5c0f\u3002\n\n \u4f60\u4e5f\u88ab\u7ed9\u4e88\u4e09\u4e2a\u6574\u6570 sr ,  sc \u548c newColor \u3002\u4f60\u5e94\u8be5\u4ece\u50cf\u7d20 image[sr][sc] \u5f00\u59cb\u5bf9\u56fe\u50cf\u8fdb\u884c \u4e0a\u8272\u586b\u5145 \u3002\n\n \u4e3a\u4e86\u5b8c\u6210 \u4e0a\u8272\u5de5\u4f5c \uff0c\u4ece\u521d\u59cb\u50cf\u7d20\u5f00\u59cb\uff0c\u8bb0\u5f55\u521d\u59cb\u5750\u6807\u7684 \u4e0a\u4e0b\u5de6\u53f3\u56db\u4e2a\u65b9\u5411\u4e0a \u50cf\u7d20\u503c\u4e0e\u521d\u59cb\u5750\u6807\u76f8\u540c\u7684\u76f8\u8fde\u50cf\u7d20\u70b9\uff0c\u63a5\u7740\u518d\u8bb0\u5f55\u8fd9\u56db\u4e2a\u65b9\u5411\u4e0a\u7b26\u5408\u6761\u4ef6\u7684\u50cf\u7d20\u70b9\u4e0e\u4ed6\u4eec\u5bf9\u5e94 \u56db\u4e2a\u65b9\u5411\u4e0a \u50cf\u7d20\u503c\u4e0e\u521d\u59cb\u5750\u6807\u76f8\u540c\u7684\u76f8\u8fde\u50cf\u7d20\u70b9\uff0c\u2026\u2026\uff0c\u91cd\u590d\u8be5\u8fc7\u7a0b\u3002\u5c06\u6240\u6709\u6709\u8bb0\u5f55\u7684\u50cf\u7d20\u70b9\u7684\u989c\u8272\u503c\u6539\u4e3a newColor \u3002\n\n \u6700\u540e\u8fd4\u56de \u7ecf\u8fc7\u4e0a\u8272\u6e32\u67d3\u540e\u7684\u56fe\u50cf \u3002\n\n\n\n \u793a\u4f8b 1:\n\n\n\n \u8f93\u5165: image = [[1,1,1],[1,1,0],[1,0,1]]\uff0csr = 1, sc = 1, newColor = 2\n \u8f93\u51fa: [[2,2,2],[2,2,0],[2,0,1]]\n \u89e3\u6790: \u5728\u56fe\u50cf\u7684\u6b63\u4e2d\u95f4\uff0c(\u5750\u6807(sr,sc)=(1,1)),\u5728\u8def\u5f84\u4e0a\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u50cf\u7d20\u70b9\u7684\u989c\u8272\u90fd\u88ab\u66f4\u6539\u62102\u3002\n \u6ce8\u610f\uff0c\u53f3\u4e0b\u89d2\u7684\u50cf\u7d20\u6ca1\u6709\u66f4\u6539\u4e3a2\uff0c\u56e0\u4e3a\u5b83\u4e0d\u662f\u5728\u4e0a\u4e0b\u5de6\u53f3\u56db\u4e2a\u65b9\u5411\u4e0a\u4e0e\u521d\u59cb\u70b9\u76f8\u8fde\u7684\u50cf\u7d20\u70b9\u3002\n \u793a\u4f8b 2:\n\n \u8f93\u5165: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, newColor = 2\n \u8f93\u51fa: [[2,2,2],[2,2,2]]\n\n\n \u63d0\u793a:\n\n m == image.length\n n == image[i].length\n 1 &lt;= m, n &lt;= 50\n 0 &lt;= image[i][j], newColor &lt; 2^16\n 0 &lt;= sr &lt; m\n 0 &lt;= sc &lt; n\n\n\n * <\/em><strong><em>@author <\/em><\/strong><em>zeroheart\n *\n *\/\n<\/em>public class Question733 {\n   public static int[][] floodFill(int[][] image, int sr, int sc, int newColor) {\n      int value = image[sr][sc];\n\n      <em>changeColor<\/em>(value, image, sr, sc, newColor);\n\n      return image;\n   }\n\n   public static int[][] changeColor(int value, int[][] image, int sr, int sc, int newColor){\n      if(newColor == image[sr][sc])return image;\/\/ \u8fd9\u662f\u5173\u952e\u7684\u9000\u51fa\u6761\u4ef6\n      int m = image.length-1;\n      int n = image[0].length-1;\n      image[sr][sc] = newColor;\n\n      if(sr>0){\n         if(value == image[sr-1][sc]){\n           <em>changeColor<\/em>(value, image, sr-1, sc, newColor);\n         }\n      }\n      if(sr&lt;m){\n         if(value == image[sr+1][sc]){\n            <em>changeColor<\/em>(value, image, sr+1, sc, newColor);\n         }\n      }\n      if(sc>0){\n         if(value == image[sr][sc-1]){\n            <em>changeColor<\/em>(value, image, sr, sc-1, newColor);\n         }\n      }\n      if(sc&lt;n){\n         if(value == image[sr][sc+1]){\n            <em>changeColor<\/em>(value, image, sr, sc+1, newColor);\n         }\n      }\n\n      return image;\n   }\n\n   public static void main(String[] args) {\n      int[][] image = {{0,1,1},{0,1,1}};\n      System.<em>out<\/em>.println(<em>floodFill<\/em>(image, 1, 1, 1));\n   }\n\n}<\/pre>\n\n\n\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/max-area-of-island\/\">695. \u5c9b\u5c7f\u7684\u6700\u5927\u9762\u79ef<\/a><\/p>\n\n\n\n<p>\u8fd9\u9898\u4e5f\u503c\u5f97\u601d\u8003\uff0c\u4e3b\u8981\u662f\u9000\u51fa\u6761\u4ef6\u90a3\u91cc\uff0c\u5176\u4ed6\u7684\u53ef\u4ee5\u60f3\u5230\u3002\u6c42\u6700\u5927\u7684\uff0c\u9700\u8981\u904d\u5386\uff0c\u4e0a\u9762\u7684\u4e00\u9898\u53ea\u662f\u9012\u5f52\u5c31\u884c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/**\n *\n 695. \u5c9b\u5c7f\u7684\u6700\u5927\u9762\u79ef\n \u7ed9\u4f60\u4e00\u4e2a\u5927\u5c0f\u4e3a m x n \u7684\u4e8c\u8fdb\u5236\u77e9\u9635 grid \u3002\n\n \u5c9b\u5c7f \u662f\u7531\u4e00\u4e9b\u76f8\u90bb\u7684 1 (\u4ee3\u8868\u571f\u5730) \u6784\u6210\u7684\u7ec4\u5408\uff0c\u8fd9\u91cc\u7684\u300c\u76f8\u90bb\u300d\u8981\u6c42\u4e24\u4e2a 1 \u5fc5\u987b\u5728 \u6c34\u5e73\u6216\u8005\u7ad6\u76f4\u7684\u56db\u4e2a\u65b9\u5411\u4e0a \u76f8\u90bb\u3002\u4f60\u53ef\u4ee5\u5047\u8bbe grid \u7684\u56db\u4e2a\u8fb9\u7f18\u90fd\u88ab 0\uff08\u4ee3\u8868\u6c34\uff09\u5305\u56f4\u7740\u3002\n\n \u5c9b\u5c7f\u7684\u9762\u79ef\u662f\u5c9b\u4e0a\u503c\u4e3a 1 \u7684\u5355\u5143\u683c\u7684\u6570\u76ee\u3002\n\n \u8ba1\u7b97\u5e76\u8fd4\u56de grid \u4e2d\u6700\u5927\u7684\u5c9b\u5c7f\u9762\u79ef\u3002\u5982\u679c\u6ca1\u6709\u5c9b\u5c7f\uff0c\u5219\u8fd4\u56de\u9762\u79ef\u4e3a 0 \u3002\n\n\n\n \u793a\u4f8b 1\uff1a\n\n\n \u8f93\u5165\uff1agrid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]\n \u8f93\u51fa\uff1a6\n \u89e3\u91ca\uff1a\u7b54\u6848\u4e0d\u5e94\u8be5\u662f 11 \uff0c\u56e0\u4e3a\u5c9b\u5c7f\u53ea\u80fd\u5305\u542b\u6c34\u5e73\u6216\u5782\u76f4\u8fd9\u56db\u4e2a\u65b9\u5411\u4e0a\u7684 1 \u3002\n \u793a\u4f8b 2\uff1a\n\n \u8f93\u5165\uff1agrid = [[0,0,0,0,0,0,0,0]]\n \u8f93\u51fa\uff1a0\n\n\n \u63d0\u793a\uff1a\n\n m == grid.length\n n == grid[i].length\n 1 &lt;= m, n &lt;= 50\n grid[i][j] \u4e3a 0 \u6216 1\n\n * <\/em><strong><em>@author <\/em><\/strong><em>zeroheart\n *\n *\/\n<\/em>public class Question695 {\n   public static int maxAreaOfIsland(int[][] grid) {\n      int m = grid.length;\n      int n = grid[0].length;\n      int max = 0;\n\n      for(int i = 0; i&lt;m; i++){\n         for(int j = 0; j&lt;n; j++){\n            if(grid[i][j] == 1) {\n               max = Math.<em>max<\/em>(max, <em>search<\/em>(grid, i, j));\n            }\n         }\n      }\n\n      return max;\n   }\n\n   public static int search(int[][] grid, int x, int y){\n      int count = 0;\n      if(x&lt;0 || x >= grid.length || y >= grid[0].length || y&lt;0 || grid[x][y] == 0) return 0;\n\n      if(grid[x][y] == 1) {\n         <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-bright-red-color\">grid[x][y] = 0; \/\/ \u589e\u52a0\u9000\u51fa\u6761\u4ef6\uff0c\u5426\u5219\u5c31\u4f1a\u6ea2\u51fa\u4e86\n         count = 1;<\/mark>\n      }\n      else return 0;\n\n      count += <em>search<\/em>(grid, x+1, y);\n      count += <em>search<\/em>(grid, x-1, y);\n      count += <em>search<\/em>(grid, x, y+1);\n      count += <em>search<\/em>(grid, x, y-1);\n\n      return count;\n   }\n\n   public static void main(String[] args) {\n      int[][] grid = {{0,0,1,0,0,0,0,1,0,0,0,0,0},{0,0,0,0,0,0,0,1,1,1,0,0,0},{0,1,1,0,1,0,0,0,0,0,0,0,0},{0,1,0,0,1,1,0,0,1,0,1,0,0},{0,1,0,0,1,1,0,0,1,1,1,0,0},{0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,0,0,0,0,0,1,1,1,0,0,0},{0,0,0,0,0,0,0,1,1,0,0,0,0}};\n\n      System.<em>out<\/em>.println(<em>maxAreaOfIsland<\/em>(grid));\n   }\n\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u300c\u7b97\u6cd5\u300d &#8211; \u5b66\u4e60\u8ba1\u5212 &#8211; \u529b\u6263\uff08LeetCode\uff09\u5168\u7403\u6781\u5ba2\u631a\u7231\u7684\u6280\u672f\u6210\u957f\u5e73\u53f0 (le [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[155],"tags":[169,168],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2531"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2531"}],"version-history":[{"count":12,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2531\/revisions"}],"predecessor-version":[{"id":2543,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2531\/revisions\/2543"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}