From 60747bfc1b1e774cb568405fdbf084529c081dde Mon Sep 17 00:00:00 2001 From: Luren3 <995171801@qq.com> Date: Sun, 19 Apr 2020 23:16:50 +0800 Subject: [PATCH 1/4] =?UTF-8?q?week06=20=E7=AE=97=E6=B3=95=E9=A2=98?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 18436 bytes .idea/$CACHE_FILE$ | 19 +++ .idea/.gitignore | 2 + .idea/algorithm007-class02.iml | 9 ++ .idea/checkstyle-idea.xml | 16 +++ .idea/codeStyles/Project.xml | 116 ++++++++++++++++++ .idea/dictionaries | 6 + .idea/encodings.xml | 6 + .idea/misc.xml | 9 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 + Week_01/.DS_Store | Bin 0 -> 6148 bytes Week_03/.DS_Store | Bin 0 -> 8196 bytes Week_04/.DS_Store | Bin 0 -> 8196 bytes Week_06/.DS_Store | Bin 0 -> 8196 bytes .../G20200343040202/LeetCode_221_0202.java | 16 +++ .../G20200343040202/LeetCode_621_0202.java | 56 +++++++++ .../G20200343040202/LeetCode_647_0202.java | 15 +++ Week_06/G20200343040202/LeetCode_64_0202.java | 20 +++ Week_06/G20200343040202/LeetCode_76_0202.java | 49 ++++++++ Week_06/G20200343040202/LeetCode_91_0202.java | 25 ++++ 21 files changed, 378 insertions(+) create mode 100644 .DS_Store create mode 100644 .idea/$CACHE_FILE$ create mode 100644 .idea/.gitignore create mode 100644 .idea/algorithm007-class02.iml create mode 100644 .idea/checkstyle-idea.xml create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/dictionaries create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Week_01/.DS_Store create mode 100644 Week_03/.DS_Store create mode 100644 Week_04/.DS_Store create mode 100644 Week_06/.DS_Store create mode 100644 Week_06/G20200343040202/LeetCode_221_0202.java create mode 100644 Week_06/G20200343040202/LeetCode_621_0202.java create mode 100644 Week_06/G20200343040202/LeetCode_647_0202.java create mode 100644 Week_06/G20200343040202/LeetCode_64_0202.java create mode 100644 Week_06/G20200343040202/LeetCode_76_0202.java create mode 100644 Week_06/G20200343040202/LeetCode_91_0202.java diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e830cf114a3235c4ba7d2cb2a0ad9910dacbbe65 GIT binary patch literal 18436 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 + + + + + + + + + + + + Android + + + + + + \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..5c98b428 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/algorithm007-class02.iml b/.idea/algorithm007-class02.iml new file mode 100644 index 00000000..d6ebd480 --- /dev/null +++ b/.idea/algorithm007-class02.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/checkstyle-idea.xml b/.idea/checkstyle-idea.xml new file mode 100644 index 00000000..e61c523d --- /dev/null +++ b/.idea/checkstyle-idea.xml @@ -0,0 +1,16 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..681f41ae --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,116 @@ + + + + + + + +
+ + + + xmlns:android + + ^$ + + + +
+
+ + + + xmlns:.* + + ^$ + + + BY_NAME + +
+
+ + + + .*:id + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + .*:name + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + name + + ^$ + + + +
+
+ + + + style + + ^$ + + + +
+
+ + + + .* + + ^$ + + + BY_NAME + +
+
+ + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + +
+
+ + + + .* + + .* + + + BY_NAME + +
+
+
+
+
+
\ No newline at end of file diff --git a/.idea/dictionaries b/.idea/dictionaries new file mode 100644 index 00000000..7dde272c --- /dev/null +++ b/.idea/dictionaries @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 00000000..97626ba4 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..51cc4e93 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..712e4eb4 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Week_01/.DS_Store b/Week_01/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..0d4294ea56757c6ef72795dc9b5f1a61ce553081 GIT binary patch literal 6148 zcmeHKF-`+P475p#NED<@xlafo4_HM(c|bmpA`%S>5)#x`@h+amjBN!)7bF@QG>+`q z^?7!6Q=DTn^Ud4w-fV7WGdR&c7{Jt{dW{&KbmUvr^}-=B>E<x8rOK<3x~v@BOi32{tQqTnH2bI1-=0C=@T9R literal 0 HcmV?d00001 diff --git a/Week_03/.DS_Store b/Week_03/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e2f9dbe7ad35c625f471561cec240cfb45927b6f GIT binary patch literal 8196 zcmeHMU2GIp6u#fKz)U;PDIlHlligY?KPk|5i!D)b`vd)<4V7(afl9l(GtvpunX)t6 z0;Qxz6JwM|41zDl7^7(90Wtn0J{S`u#usXgM13$OzL;p@Phx!W+_|$=S_%&)2E*Lu z-gEE$&bfEb>33#&wu~_}=k!{}G{%@jolCWthFdg_^FFUdf}KK=AbZAY=qJwVsn^WY z4mZLGgb@fM5Jn)3Kp26$Ap-Qy=1p4Qy)V#k3?mRm;GT>Ce?G*hbD0R_w2=Ozg9g6@ zAjwMrztNoP0N*AS$V4Egh4fA7Oi?``Fhx*eK$sIA@ z$u8!rGbDrz$1nn61m;J8r%wrMV|7%q^Zs}JULWk#r6=66wB@8p^ICe$GF`8x=1W8h z3KuP2B1uJ3(bA&FlOxVV(o1gzP#8*8jZzLVedmYD`ou|_KO=Dt6UD@K;UdptM ztZ!`U2B{48o3`#uG&?z4cl~pc0+1wGl84mt$;pOzV|A>yv3{yLHd!05i&w`Qwr!i5 zlBKe$+8rIkqbE*Io|-y+Ubu?jaX~)b+%C}a4t`tic#H5maw+fkjlAD-QL(aAYwu|9 z+@lPs%g4#3yyjK&OI!*=N5-*xvIfWdGme!TwcR1LG?TJ2IVf-YC+D2JTNxd$)C3dQnEwbv$y+`TSE=+5cxTNZGpOs1*me_hoX_VAGa{su9 ztk`^5*&&spS{}$aqoee>U9p|enkDtBbeP}Wm2$nkV}>1EuSK=pl5~J0O}cLAQe~bJ zm5FLG^?ig!`-T0= z{y+o;ScJtWfrctPhz;0?I>b?r1Uk@(E_7ob4q*_7F$5i>urY>Xn7}li#8Y@0&*Lm! zz>7GKSMVm@!rQonk8l;A;u>b~1-``(_z}N}4;aa_yIZXDBYvmZo!K2^b>!@=*&S_b zZ|ms1i`o6vGEV6gEALxX7OUF0xvt@nrdc-3A;3*M{Uy@awh=?!H#_fut7E)#ibjMCY7gG)hw7G>)#&cBumn6Q%uCwomz<+`fZHuvj zh+Bd6*o+1|iYByT7j|PW9zznn=<{hij5Ln=)OBziCvgf>IE`oUES@9!o+0|4Bl=#z zOL!Hp;dQ)$i@1y{cn|O61AK^2@HwvI>p2we%9B?}d~;_Ck0-OX<=96^GoJ`I9}=!^ z8-W6WYP|m6wea`={S!w7^C_ea1a#~12sQu?(1Q>ew{-6CLHhlm4B)kWQzW{>mB_aR- literal 0 HcmV?d00001 diff --git a/Week_04/.DS_Store b/Week_04/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..017ae2cdc7f0d375048534e1f73662f757a5e01c GIT binary patch literal 8196 zcmeHMZEO@p7@p@^V7Dyowia5J0w+wOjHs-@h6(#M~FW(#wLUhfB3^6#>77wO!SZM?Cc>0YKb9+#GPj5 zoq6AR-yrFYgmQ|gb4gE5#Z-Tlrop`K#mLPA01TqPXLmo zr1lfl=^fzvL<1QQ&0Sc5h*F1 zF>{tAl}TlD${tP)Iio2r?d9FJbZ#eab*8W0Y#mRA1fFGBzelHA|b; zx9=W4aPZLKu_G^wg(5gCXoZv0E7Y83)z)BBD(ei71dhrLc({dxz$x;65vTJ-oaD0E z${cNT`{pf5N3WT6?38)X7*H!*f~uXGojIdGu=HddD?e*H~;SouVGP2KHFm*2Z`?E{I43bnFIsaEFo6inC5SVs4#se40xX*X-wx@iyg z(+6aG<{s0?$(1GLa+jSRHYTdn%8J_gn%0}on2+X7?|@n_Ng~!FB`T{Q%e!=%$u>vl zp1R+E$s$eL$+d}`L8VR(JWRtfP3dw80W~ejzUHn*Md{i{Oi^7~Xktx@)@`LThNbGV zaBEqmXgvk7G<9pBTz8+M_0q>WOjC9%(HN*V#5L^^K5|Fe^|p@~wxO$Mg5i@it3GaP z)pb56sXm|ttB*H|s=s7e<#*UZ*1*=#oz=zmFozvwPqE|dBs;}EVPCQD*ah|j`p5*@eH2B z^EifA@h0BF+js{b;WR$S7x)t2;2h550xsf5{DNQco0w;$xF5C^mmnlAk(_QnxP{;p zh`e}xcGvmrZn)*yeXg8S`nKx1^J=0?m*2Bu&HA>>l(>onlQ?=MHGMt`!udoj5>1ew zh`*q2p|W_YEz?cZHTFxKf9k9WzxR^5J0zl}1UnNR4iaTXV^qHxDm6#jmC3J*KUWN?v5ClXKZ z&DNf7y%8_{UwDlSU3q=@UrMLhV1-o9wj6tqW>nm!lb3kYTO}f5Er0)Socj0wn?7zB zFhn3k;MPF^rO8yXjpj4exf3BrHS&NMe-a;z2@>NAYm6lNU`%|`XreJOzIg83*(xoh4<-h~+|9k` z&iT%{ckbzTW_orRV`wWFjg0AxF-~1bt&)a2G=Ymgqs2l^IZ2Q|V-56aW-ZhkW>`mx zC<0Lgq6kD0h$0Y0;9iITy|YE*cX{tiG#aA_L=m_zBOsm+3F<;7LpdR3@aUi+C;>={ z5+G>O>2wWUB}N@j+qaf zEyE;}qkWcRxT9@u!7;pGosUu5o+U z;Lw?~W9P=tUy=t!_^_Z8-rg?Le!Xisc)4|tyEO1^YzZI&OK^(T3a|m z2iiaETdw0BGV>nEv{vO&5V~@%b0BXDyf5e4g(1gFYt^}ooh#TG-)yz*ly%l5U6q&1 zyS9D6^(?W>;eOvd?F(!M)PKZt^L~4#M(H)45K);4=bEyF)?IOF9^$FQ8^M<~vYZ#`+5d8N8+fzsn- zhRncAU#c(XS}pG{WUW&L%Rf_GSglp9RO9MVgSOn~8p6v^w09M0AJ-q^d_Z9Bd6Py% zZoXPyt7sYCmB)YoprCHACsOGg;G?s?F+FuI*L&Mr35A@)31A zuSUJnpL2(X=)OIvPU>x3yUve^y}L7>zkk?t!tM3AzKiq20%y)V4qP~dl(%+BsGr(+lI%)4X23;Bwt$2sl1cLGOP4KQK|JpzdYY%F zCrl}kw0VMvyeOIXxb fCqgV4$_Xh6q4uAD5fHuqr~ZkJ-v0pw@5|vYJT@cj literal 0 HcmV?d00001 diff --git a/Week_06/G20200343040202/LeetCode_221_0202.java b/Week_06/G20200343040202/LeetCode_221_0202.java new file mode 100644 index 00000000..e5892482 --- /dev/null +++ b/Week_06/G20200343040202/LeetCode_221_0202.java @@ -0,0 +1,16 @@ +class Solution { + public int maximalSquare(char[][] matrix) { + int rows = matrix.length, cols = rows > 0 ? matrix[0].length : 0; + int[][] dp = new int[rows + 1][cols + 1]; + int maxsqlen = 0; + for (int i = 1; i <= rows; i++) { + for (int j = 1; j <= cols; j++) { + if (matrix[i-1][j-1] == '1'){ + dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1; + maxsqlen = Math.max(maxsqlen, dp[i][j]); + } + } + } + return maxsqlen * maxsqlen; + } +} \ No newline at end of file diff --git a/Week_06/G20200343040202/LeetCode_621_0202.java b/Week_06/G20200343040202/LeetCode_621_0202.java new file mode 100644 index 00000000..684b2087 --- /dev/null +++ b/Week_06/G20200343040202/LeetCode_621_0202.java @@ -0,0 +1,56 @@ +class Solution { + public int[][] imageSmoother(int[][] M) { + int r = M.length; + int c = M[0].length; + int[][] res = new int[r][c]; + for(int i = 0; i < r;i++){ + for(int j = 0;j < c;j++){ + res[i][j] = add(M,i,j,r,c); + } + } + return res; + } + + private int add(int[][] M,int i,int j,int m ,int n){ + int res = 0; + int num = 0; + if(i-1>=0){ + res += M[i-1][j]; + num++; + } + if(i+1 < m){ + res += M[i+1][j]; + num++; + } + if(j-1 >= 0){ + res += M[i][j-1]; + num++; + } + if(j+1 < n){ + res += M[i][j+1]; + num++; + } + + if(i+1 < m &&j-1 >=0){ + res += M[i+1][j-1]; + num++; + } + if(i-1>=0&&j-1>=0){ + res += M[i-1][j-1]; + num++; + } + if(i-1>=0&&j+1= 0; i--) { + if (s.charAt(i) == s.charAt(j) && ((j - i < 2) || dp[i + 1][j - 1])) { + dp[i][j] = true; + res++; + } + } + } + return res; + } +} \ No newline at end of file diff --git a/Week_06/G20200343040202/LeetCode_64_0202.java b/Week_06/G20200343040202/LeetCode_64_0202.java new file mode 100644 index 00000000..99007ef0 --- /dev/null +++ b/Week_06/G20200343040202/LeetCode_64_0202.java @@ -0,0 +1,20 @@ +class Solution { + public int minPathSum(int[][] grid) { + + int dp[][] = new int[grid.length][grid[0].length]; + dp[0][0] = grid[0][0]; + + for (int i = 1; i < grid.length; i++) { + dp[i][0] = grid[i][0] + dp[i - 1][0]; + } + for (int i = 1; i < grid[0].length; i++) { + dp[0][i] = grid[0][i] + dp[0][i - 1]; + } + for (int i = 1; i < grid.length; i++) { + for (int j = 1; j < grid[0].length; j++) { + dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]; + } + } + return dp[grid.length - 1][grid[0].length - 1]; + } +} \ No newline at end of file diff --git a/Week_06/G20200343040202/LeetCode_76_0202.java b/Week_06/G20200343040202/LeetCode_76_0202.java new file mode 100644 index 00000000..af0c5ef1 --- /dev/null +++ b/Week_06/G20200343040202/LeetCode_76_0202.java @@ -0,0 +1,49 @@ +class Solution { + public String minWindow(String s, String t) { + if (s == null || s == "" || t == null || t == "" || s.length() < t.length()) { + return ""; + } + + int[] needs = new int[128]; + int[] window = new int[128]; + + for (int i = 0; i < t.length(); i++) { + needs[t.charAt(i)]++; + } + + int left = 0; + int right = 0; + + String res = ""; + + int count = 0; + + int minLength = s.length() + 1; + + while (right < s.length()) { + char ch = s.charAt(right); + window[ch]++; + if (needs[ch] > 0 && needs[ch] >= window[ch]) { + count++; + } + + while (count == t.length()) { + ch = s.charAt(left); + if (needs[ch] > 0 && needs[ch] >= window[ch]) { + count--; + } + if (right - left + 1 < minLength) { + minLength = right - left + 1; + res = s.substring(left, right + 1); + + } + window[ch]--; + left++; + + } + right++; + + } + return res; + } +} \ No newline at end of file diff --git a/Week_06/G20200343040202/LeetCode_91_0202.java b/Week_06/G20200343040202/LeetCode_91_0202.java new file mode 100644 index 00000000..d589bf38 --- /dev/null +++ b/Week_06/G20200343040202/LeetCode_91_0202.java @@ -0,0 +1,25 @@ +class Solution { + public int numDecodings(String s) { + char[] arr = s.toCharArray(); + int[] dp = new int[s.length() + 1]; + dp[0] = 1; + dp[1] = arr[0] == '0' ? 0 : 1; + if (s.length() <= 1) return dp[1]; + for (int i = 2; i <= s.length(); i++) { + int n = (arr[i - 2] - '0') * 10 + (arr[i - 1] - '0'); + if (arr[i - 1] == '0' && arr[i - 2] == '0') { + return 0; + } else if (arr[i - 2] == '0') { + dp[i] = dp[i - 1]; + } else if (arr[i - 1] == '0') { + if (n > 26) return 0; + dp[i] = dp[i - 2]; + } else if (n > 26) { + dp[i] = dp[i - 1]; + } else { + dp[i] = dp[i - 1] + dp[i - 2]; + } + } + return dp[dp.length - 1]; + } +} \ No newline at end of file From cc4b3b10011e3b2974a020d8148dfc802d640625 Mon Sep 17 00:00:00 2001 From: Luren3 <995171801@qq.com> Date: Sun, 26 Apr 2020 20:02:44 +0800 Subject: [PATCH 2/4] =?UTF-8?q?week07=20=E7=AE=97=E6=B3=95=E9=A2=98?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../G20200343040202/LeetCode_127_0202.java | 91 +++++++++++++++++++ .../G20200343040202/LeetCode_130_0202.java | 29 ++++++ .../G20200343040202/LeetCode_200_0202.java | 28 ++++++ .../G20200343040202/LeetCode_208_0202.java | 62 +++++++++++++ .../G20200343040202/LeetCode_212_0202.java | 48 ++++++++++ Week_07/G20200343040202/LeetCode_22_0202.java | 23 +++++ Week_07/G20200343040202/LeetCode_36_0202.java | 22 +++++ .../G20200343040202/LeetCode_433_0202.java | 48 ++++++++++ .../G20200343040202/LeetCode_547_0202.java | 51 +++++++++++ Week_07/G20200343040202/LeetCode_70_0202.java | 24 +++++ 10 files changed, 426 insertions(+) create mode 100644 Week_07/G20200343040202/LeetCode_127_0202.java create mode 100644 Week_07/G20200343040202/LeetCode_130_0202.java create mode 100644 Week_07/G20200343040202/LeetCode_200_0202.java create mode 100644 Week_07/G20200343040202/LeetCode_208_0202.java create mode 100644 Week_07/G20200343040202/LeetCode_212_0202.java create mode 100644 Week_07/G20200343040202/LeetCode_22_0202.java create mode 100644 Week_07/G20200343040202/LeetCode_36_0202.java create mode 100644 Week_07/G20200343040202/LeetCode_433_0202.java create mode 100644 Week_07/G20200343040202/LeetCode_547_0202.java create mode 100644 Week_07/G20200343040202/LeetCode_70_0202.java diff --git a/Week_07/G20200343040202/LeetCode_127_0202.java b/Week_07/G20200343040202/LeetCode_127_0202.java new file mode 100644 index 00000000..9bdd7f4d --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_127_0202.java @@ -0,0 +1,91 @@ +class Solution { + + public int ladderLength(String beginWord, String endWord, List wordList) { + HashSet start = new HashSet<>(); + start.add(beginWord); + HashSet end = new HashSet<>(); + end.add(endWord); + HashSet words = new HashSet<>(wordList); + if (!words.contains(endWord)) { + return 0; + } + + return deBfs(start, end, words, 2); + } + + private int deBfs(HashSet start, HashSet end, HashSet words, int depth) { + + if (start.size() > end.size()) { + return deBfs(end, start, words, depth); + } + words.removeAll(start); + HashSet next = new HashSet<>(); + for (String str : start) { + char[] chars = str.toCharArray(); + for (int i = 0; i < chars.length; i++) { + char temp = chars[i]; + for (char j = 'a'; j <= 'z'; j++) { + chars[i] = j; + String word = new String(chars); + if (words.contains(word)) { + if (end.contains(word)) { + return depth; + } + next.add(word); + } + } + chars[i] = temp; + } + } + if (start.isEmpty()) { + return 0; + } + return deBfs(next, end, words, depth + 1); + + } +// public int ladderLength(String beginWord, String endWord, List wordList) { +// if (!wordList.contains(endWord)) +// return 0; +// +// Set visited = new HashSet<>(); +// Queue queue = new LinkedList<>(); +// queue.add(beginWord); +// +// int res = 0; +// while (!queue.isEmpty()) { +// res++; +// for (int i = 0; i < queue.size(); i++) { +// String start = queue.poll(); +// for (String string : wordList) { +// if (visited.contains(string)) { +// continue; +// } +// if (!progress(start, string)) { +// continue; +// } +// if (string.equals(endWord)) { +// return res + 1; +// } +// visited.add(string); +// queue.offer(string); +// } +// } +// } +// return 0; +// } +// private boolean progress(String s1, String s2) { +// if (s1.length() != s2.length()) { +// return false; +// } +// int count = 0; +// for (int i = 0; i < s1.length(); i++) { +// if (s1.charAt(i) != s2.charAt(i)) { +// count++; +// if (count > 1) { +// return false; +// } +// } +// } +// return count == 1; +// } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_130_0202.java b/Week_07/G20200343040202/LeetCode_130_0202.java new file mode 100644 index 00000000..5ed4b309 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_130_0202.java @@ -0,0 +1,29 @@ +class Solution { + + public void solve(char[][] board) { + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[i].length; j++) { + boolean isEdge = i == 0 || j == 0 || i == board.length - 1 || j == board[i].length - 1; + if (isEdge && board[i][j] == 'O') { + solveDfs(board, i, j); + } + } + } + + for(int i=0;i= board.length || j < 0 || j >= board[0].length || board[i][j] == 'X' || board[i][j] == '1') { + return; + } + board[i][j] = '1'; + solveDfs(board, i - 1, j); + solveDfs(board, i + 1, j); + solveDfs(board, i, j - 1); + solveDfs(board, i, j + 1); + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_200_0202.java b/Week_07/G20200343040202/LeetCode_200_0202.java new file mode 100644 index 00000000..dc718ed6 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_200_0202.java @@ -0,0 +1,28 @@ +class Solution { + + public int numIslands(char[][] grid) { + + int count = 0; + + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[i].length; j++) { + if (grid[i][j] == '1') { + count++; + dfs(grid, i, j); + } + } + } + return count; + } + + private void dfs(char[][] grid, int i, int j) { + if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == '0') { + return; + } + grid[i][j] = '0'; + dfs(grid, i - 1, j); + dfs(grid, i + 1, j); + dfs(grid, i, j - 1); + dfs(grid, i, j + 1); + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_208_0202.java b/Week_07/G20200343040202/LeetCode_208_0202.java new file mode 100644 index 00000000..3ade0388 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_208_0202.java @@ -0,0 +1,62 @@ +public class Trie { + + private TreeNode node; + + /** Initialize your data structure here. */ + public Trie() { + node = new TreeNode(); + } + + /** Inserts a word into the trie. */ + public void insert(String word) { + TreeNode node = this.node; + for (char c:word.toCharArray()){ + if (node.child[c - 'a'] == null){ + node.child[c - 'a'] = new TreeNode(); + } + node = node.child[c - 'a']; + } + node.value = word; + } + + /** Returns if the word is in the trie. */ + public boolean search(String word) { + TreeNode node = this.node; + for (char c:word.toCharArray()){ + if (node.child[c - 'a'] == null){ + return false; + } + node = node.child[c - 'a']; + } + return node.value.equals(word); + } + + /** Returns if there is any word in the trie that starts with the given prefix. */ + public boolean startsWith(String prefix) { + TreeNode node = this.node; + for (char c:prefix.toCharArray()){ + if (node.child[c - 'a'] == null){ + return false; + } + node = node.child[c - 'a']; + } + return true; + } + + public TreeNode next(char c){ + if ( 'a' <= c && c <= 'z'){ + return node.child[c]; + } + return null; + } + + class TreeNode{ + String value; + TreeNode[] child; + + public TreeNode(){ + value = ""; + child = new TreeNode[26]; + } + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_212_0202.java b/Week_07/G20200343040202/LeetCode_212_0202.java new file mode 100644 index 00000000..0028e3d0 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_212_0202.java @@ -0,0 +1,48 @@ +class Solution { + + public List findWords(char[][] board, String[] words) { + List list = new ArrayList<>(); + for (String word : words) { + if (exist(board, word) && !list.contains(word)) { + list.add(word); + } + } + return list; + } + + private boolean exist(char[][] board, String word) { + if (word.length() == 0 || board.length == 0) { + return false; + } + + boolean[][] flag = new boolean[board.length][board[0].length]; + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[i].length; j++) { + if (board[i][j] == word.charAt(0)) { + if (find(board, word, 0, i, j, flag)) { + return true; + } + } + } + } + return false; + } + + private boolean find(char[][] board, String word, int index, int i, int j, boolean[][] flag) { + if (index == word.length()) { + return true; + } + boolean result = false; + if (i >= 0 && i < board.length && j >= 0 && j < board[i].length && !flag[i][j] && word.charAt(index) == board[i][j]) { + flag[i][j] = true; + index += 1; + result = find(board, word, index, i + 1, j, flag) || find(board, word, index, i - 1, j, flag) || + find(board, word, index, i, j + 1, flag) || find(board, word, index, i, j - 1, flag); + if (result == false) { + index -= 1; + flag[i][j] = false; + } + } + return result; + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_22_0202.java b/Week_07/G20200343040202/LeetCode_22_0202.java new file mode 100644 index 00000000..aa0d6a17 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_22_0202.java @@ -0,0 +1,23 @@ +class Solution { + + public List generateParenthesis(int n) { + List list = new ArrayList<>(); + generate(0,0,n,"",list); + return list; + } + + private void generate(int left, int right, int n,String s, List list) { + + //条件 + if (left == n && right == n){ + list.add(s); + return; + } + + if (left < n) + generate(left+1,right,n,s+"(",list); + if (left > right) + generate(left,right+1,n,s+")",list); + + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_36_0202.java b/Week_07/G20200343040202/LeetCode_36_0202.java new file mode 100644 index 00000000..35493e97 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_36_0202.java @@ -0,0 +1,22 @@ +class Solution { + + public boolean isValidSudoku(char[][] board) { + int[][] rows = new int[9][10]; + int[][] cols = new int[9][10]; + int[][] boxs = new int[9][10]; + + for (int i = 0; i < 9; ++i) { + for (int j = 0; j < 9; ++j) { + if (board[i][j] == '.') continue; + int num = board[i][j] - '0'; + rows[i][num]++; + cols[j][num]++; + boxs[(i/3)*3 + j/3][num]++; + if (rows[i][num] + cols[j][num] + boxs[(i/3)*3 + j/3][num] > 3) { + return false; + } + } + } + return true; + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_433_0202.java b/Week_07/G20200343040202/LeetCode_433_0202.java new file mode 100644 index 00000000..4561881d --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_433_0202.java @@ -0,0 +1,48 @@ +class Solution { + + public int minMutation(String start, String end, String[] bank) { + + boolean[] visited = new boolean[bank.length]; + Queue queue = new LinkedList<>(); + queue.add(start); + //深度 + int res = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + res++; + for (int i = 0; i < size; i++) { + String poll = queue.poll(); + for (int j = 0; j < bank.length; j++) { + //已经访问过的元素跳过,树中不存在重复节点 + if (visited[j]) { + continue; + } + //孩子节点只能改变一个字符转换为s,不满足则跳过 + String string = bank[j]; + if (!progress(poll, string)) { + continue; + } + //如果节点s等于endWord,接龙完成 + if (string.equals(end)) { + return res; + } + visited[j] = true; + queue.add(string); + } + } + } + return 0; + } + private boolean progress(String poll, String s) { + int count = 0; + for (int i = 0; i < s.length(); i++) { + if (poll.charAt(i) != s.charAt(i)) { + count++; + if (count > 1) { + return false; + } + } + } + return count == 1; + } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_547_0202.java b/Week_07/G20200343040202/LeetCode_547_0202.java new file mode 100644 index 00000000..8af7b2f7 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_547_0202.java @@ -0,0 +1,51 @@ +class Solution { + + public int find(int parent[], int i) { + if (parent[i] == -1) + return i; + return find(parent, parent[i]); + } + public void union(int parent[], int x, int y) { + int xset = find(parent, x); + int yset = find(parent, y); + if (xset != yset) + parent[xset] = yset; + } + public int findCircleNum(int[][] M) { + int[] parent = new int[M.length]; + Arrays.fill(parent, -1); + for (int i = 0; i < M.length; i++) { + for (int j = 0; j < M.length; j++) { + if (M[i][j] == 1 && i != j) { + union(parent, i, j); + } + } + } + int count = 0; + for (int i = 0; i < parent.length; i++) { + if (parent[i] == -1) + count++; + } + return count; + } + + // public int findCircleNum(int[][] M) { + // int[] visited = new int[M.length]; + // int count = 0; + // for (int i = 0; i < M.length; i++) { + // if (visited[i] == 0) { + // findCircleNumDfs(M, visited, i); + // count++; + // } + // } + // return count; + // } + // private void findCircleNumDfs(int[][] M, int[] visited, int i) { + // for (int j = 0; j < M.length; j++) { + // if (M[i][j] == 1 && visited[j] == 0) { + // visited[j] = 1; + // findCircleNumDfs(M, visited, j); + // } + // } + // } +} \ No newline at end of file diff --git a/Week_07/G20200343040202/LeetCode_70_0202.java b/Week_07/G20200343040202/LeetCode_70_0202.java new file mode 100644 index 00000000..9e1e5fd7 --- /dev/null +++ b/Week_07/G20200343040202/LeetCode_70_0202.java @@ -0,0 +1,24 @@ +class Solution { + public int climbStairs(int n) { + if (n == 1) return 1; + if (n == 2) return 2; + int p = 2, q = 1; + for (int i = 2; i < n; i++) { + int sum = p + q; + q = p; + p = sum; + } + return p; + } +// public int climbStairs(int n) { +// if (n == 1) return 1; +// if (n == 2) return 2; +// int[] dp = new int[n + 1]; +// dp[1] = 1; +// dp[2] = 2; +// for (int i = 3; i <= n; i++) { +// dp[i] = dp[i - 1] + dp[i - 2]; +// } +// return dp[n]; +// } +} \ No newline at end of file From feb0f8908845ebb2386d2f79b0345152ccfaa200 Mon Sep 17 00:00:00 2001 From: Luren3 <995171801@qq.com> Date: Sun, 10 May 2020 20:23:28 +0800 Subject: [PATCH 3/4] =?UTF-8?q?week08=20=E7=AE=97=E6=B3=95=E9=A2=98?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../G20200343040202/LeetCode_1122_0202.java | 25 +++++ .../G20200343040202/LeetCode_146_0202.java | 104 ++++++++++++++++++ .../G20200343040202/LeetCode_190_0202.java | 12 ++ .../G20200343040202/LeetCode_191_0202.java | 11 ++ .../G20200343040202/LeetCode_231_0202.java | 11 ++ .../G20200343040202/LeetCode_242_0202.java | 15 +++ Week_08/G20200343040202/LeetCode_56_0202.java | 31 ++++++ 7 files changed, 209 insertions(+) create mode 100644 Week_08/G20200343040202/LeetCode_1122_0202.java create mode 100644 Week_08/G20200343040202/LeetCode_146_0202.java create mode 100644 Week_08/G20200343040202/LeetCode_190_0202.java create mode 100644 Week_08/G20200343040202/LeetCode_191_0202.java create mode 100644 Week_08/G20200343040202/LeetCode_231_0202.java create mode 100644 Week_08/G20200343040202/LeetCode_242_0202.java create mode 100644 Week_08/G20200343040202/LeetCode_56_0202.java diff --git a/Week_08/G20200343040202/LeetCode_1122_0202.java b/Week_08/G20200343040202/LeetCode_1122_0202.java new file mode 100644 index 00000000..f625a46f --- /dev/null +++ b/Week_08/G20200343040202/LeetCode_1122_0202.java @@ -0,0 +1,25 @@ +class Solution { + + public int[] relativeSortArray(int[] arr1, int[] arr2) { + int[] temp = new int[1001]; + for (int i : arr1) { + temp[i]++; + } + int index=0; + for (int j : arr2) { + while (temp[j] >0) { + arr1[index] = j; + temp[j]--; + index++; + } + } + for (int i=0; i0) { + arr1[index] = i; + temp[i]--; + index++; + } + } + return arr1; + } +} \ No newline at end of file diff --git a/Week_08/G20200343040202/LeetCode_146_0202.java b/Week_08/G20200343040202/LeetCode_146_0202.java new file mode 100644 index 00000000..000cef9f --- /dev/null +++ b/Week_08/G20200343040202/LeetCode_146_0202.java @@ -0,0 +1,104 @@ +class LRUCache { + + /** + * 链表节点 + */ + class CacheNode { + CacheNode prev;//前一节点 + CacheNode next;//后一节点 + int value;//值 + int key;//键 + CacheNode() { + } + } + public LRUCache(int capacity) { + currentSize = 0; + cacheSize = capacity; + nodes = new LinkedHashMap();//缓存容器 + } + + /** + * 获取缓存中对象 + * @param key + * @return + */ + public int get(int key) { + CacheNode node = (CacheNode) nodes.get(key); + if (node != null) { + moveToHead(node); + return node.value; + } else { + return -1; + } + } + + /** + * 添加缓存 + * @param key + * @param value + */ + public void put(int key, int value) { + CacheNode node = (CacheNode) nodes.get(key); + + if (node == null) { + //缓存容器是否已经超过大小. + if (currentSize >= cacheSize) { + if (last != null)//将最少使用的删除 + nodes.remove(last.key); + removeLast(); + } else { + currentSize++; + } + + node = new CacheNode(); + } + node.value = value; + node.key = key; + //将最新使用的节点放到链表头,表示最新使用的. + moveToHead(node); + nodes.put(key, node); + } + + /** + * 删除链表尾部节点 + * 表示 删除最少使用的缓存对象 + */ + private void removeLast() { + //链表尾不为空,则将链表尾指向null. 删除连表尾(删除最少使用的缓存对象) + if (last != null) { + if (last.prev != null) + last.prev.next = null; + else + first = null; + last = last.prev; + } + } + + /** + * 移动到链表头,表示这个节点是最新使用过的 + * @param node + */ + private void moveToHead(CacheNode node) { + if (node == first) + return; + if (node.prev != null) + node.prev.next = node.next; + if (node.next != null) + node.next.prev = node.prev; + if (last == node) + last = node.prev; + if (first != null) { + node.next = first; + first.prev = node; + } + first = node; + node.prev = null; + if (last == null) + last = first; + } + private int cacheSize; + private LinkedHashMap nodes;//缓存容器 + private int currentSize; + private CacheNode first;//链表头 + private CacheNode last;//链表尾 +} \ No newline at end of file diff --git a/Week_08/G20200343040202/LeetCode_190_0202.java b/Week_08/G20200343040202/LeetCode_190_0202.java new file mode 100644 index 00000000..2885bb3c --- /dev/null +++ b/Week_08/G20200343040202/LeetCode_190_0202.java @@ -0,0 +1,12 @@ +class Solution { + + public static int reverseBits(int n) { + int result = 0; + for (int i = 0; i < Integer.SIZE; i++) { + result <<= 1; + result |= n & 1; + n >>>= 1; + } + return result; + } +} \ No newline at end of file diff --git a/Week_08/G20200343040202/LeetCode_191_0202.java b/Week_08/G20200343040202/LeetCode_191_0202.java new file mode 100644 index 00000000..8f673deb --- /dev/null +++ b/Week_08/G20200343040202/LeetCode_191_0202.java @@ -0,0 +1,11 @@ +class Solution { + + public static int hammingWeight(int n) { + int count = 0; + while (n != 0) { + count++; + n = n & (n - 1); + } + return count; + } +} \ No newline at end of file diff --git a/Week_08/G20200343040202/LeetCode_231_0202.java b/Week_08/G20200343040202/LeetCode_231_0202.java new file mode 100644 index 00000000..6da415ab --- /dev/null +++ b/Week_08/G20200343040202/LeetCode_231_0202.java @@ -0,0 +1,11 @@ +class Solution { + + public boolean isPowerOfTwo(int n) { + if (n == 0) + return false; + while (n % 2 == 0) { + n /= 2; + } + return n == 1; + } +} \ No newline at end of file diff --git a/Week_08/G20200343040202/LeetCode_242_0202.java b/Week_08/G20200343040202/LeetCode_242_0202.java new file mode 100644 index 00000000..a44131df --- /dev/null +++ b/Week_08/G20200343040202/LeetCode_242_0202.java @@ -0,0 +1,15 @@ +class Solution { + + public static boolean isAnagram(String s, String t) { + if (s.length() != t.length()) return false; + char[] chars1 = s.toCharArray(); + Arrays.sort(chars1); + char[] chars2 = t.toCharArray(); + Arrays.sort(chars2); + + if (!String.valueOf(chars1).equals(String.valueOf(chars2))) { + return false; + } + return true; + } +} \ No newline at end of file diff --git a/Week_08/G20200343040202/LeetCode_56_0202.java b/Week_08/G20200343040202/LeetCode_56_0202.java new file mode 100644 index 00000000..5ff2d568 --- /dev/null +++ b/Week_08/G20200343040202/LeetCode_56_0202.java @@ -0,0 +1,31 @@ +class Solution { + + public int[][] merge(int[][] intervals) { + if (intervals == null || intervals.length <= 1) { + return intervals; + } + int mergeCount = 0; + for (int i = 0; i < intervals.length; i++) { + for (int j = i + 1; j < intervals.length; j++) { + if (intervals[i][1] >= intervals[j][0] && intervals[i][0] <= intervals[j][1]) { + if (intervals[i][1] > intervals[j][1]) { + intervals[j][1] = intervals[i][1]; + } + if (intervals[i][0] < intervals[j][0]) { + intervals[j][0] = intervals[i][0]; + } + intervals[i] = null; + mergeCount++; + break; + } + } + } + int[][] result = new int[intervals.length - mergeCount][]; + for (int i = 0, j = 0; j < intervals.length; j++) { + if (intervals[j] != null) { + result[i++] = intervals[j]; + } + } + return result; + } +} \ No newline at end of file From 7a380e659d749b14bc29420e28dd8ec42b62c4d2 Mon Sep 17 00:00:00 2001 From: Luren3 <995171801@qq.com> Date: Sun, 17 May 2020 20:28:16 +0800 Subject: [PATCH 4/4] =?UTF-8?q?week09=20=E7=AE=97=E6=B3=95=E9=A2=98?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../G20200343040202/LeetCode_151_0202.java | 13 +++++++ .../G20200343040202/LeetCode_205_0202.java | 18 +++++++++ .../G20200343040202/LeetCode_300_0202.java | 16 ++++++++ .../G20200343040202/LeetCode_387_0202.java | 32 ++++++++++++++++ .../G20200343040202/LeetCode_541_0202.java | 15 ++++++++ .../G20200343040202/LeetCode_557_0202.java | 22 +++++++++++ .../G20200343040202/LeetCode_680_0202.java | 18 +++++++++ Week_09/G20200343040202/LeetCode_8_0202.java | 38 +++++++++++++++++++ .../G20200343040202/LeetCode_917_0202.java | 18 +++++++++ Week_09/G20200343040202/LeetCode_91_0202.java | 21 ++++++++++ 10 files changed, 211 insertions(+) create mode 100644 Week_09/G20200343040202/LeetCode_151_0202.java create mode 100644 Week_09/G20200343040202/LeetCode_205_0202.java create mode 100644 Week_09/G20200343040202/LeetCode_300_0202.java create mode 100644 Week_09/G20200343040202/LeetCode_387_0202.java create mode 100644 Week_09/G20200343040202/LeetCode_541_0202.java create mode 100644 Week_09/G20200343040202/LeetCode_557_0202.java create mode 100644 Week_09/G20200343040202/LeetCode_680_0202.java create mode 100644 Week_09/G20200343040202/LeetCode_8_0202.java create mode 100644 Week_09/G20200343040202/LeetCode_917_0202.java create mode 100644 Week_09/G20200343040202/LeetCode_91_0202.java diff --git a/Week_09/G20200343040202/LeetCode_151_0202.java b/Week_09/G20200343040202/LeetCode_151_0202.java new file mode 100644 index 00000000..22ac0bb8 --- /dev/null +++ b/Week_09/G20200343040202/LeetCode_151_0202.java @@ -0,0 +1,13 @@ +class Solution { + + public String reverseWords(String s) { + String[] arr = s.trim().split("\\s+"); + + StringBuilder builder = new StringBuilder(); + + for (int i = arr.length - 1; i >= 0; i--) { + builder.append(arr[i]).append(" "); + } + return builder.toString().trim(); + } +} \ No newline at end of file diff --git a/Week_09/G20200343040202/LeetCode_205_0202.java b/Week_09/G20200343040202/LeetCode_205_0202.java new file mode 100644 index 00000000..a4524511 --- /dev/null +++ b/Week_09/G20200343040202/LeetCode_205_0202.java @@ -0,0 +1,18 @@ +class Solution { + public boolean isIsomorphic(String s, String t) { + int[] arr1 = new int[256]; + int[] arr2 = new int[256]; + char[] s1 = s.toCharArray(); + char[] t1 = t.toCharArray(); + for (int i = 0; i < s1.length; i++) { + char c1 = s1[i]; + char c2 = t1[i]; + if (arr1[c1] != arr2[c2]) { + return false; + } + arr1[c1] = i + 1; + arr2[c2] = i + 1; + } + return true; + } +} \ No newline at end of file diff --git a/Week_09/G20200343040202/LeetCode_300_0202.java b/Week_09/G20200343040202/LeetCode_300_0202.java new file mode 100644 index 00000000..63b29b4e --- /dev/null +++ b/Week_09/G20200343040202/LeetCode_300_0202.java @@ -0,0 +1,16 @@ +class Solution { + public int lengthOfLIS(int[] nums) { + int[] dp = new int[nums.length]; + int max = 0; + for (int i=0;i map = new HashMap<>(); +// for (int i=0;i Integer.MAX_VALUE) return Integer.MAX_VALUE; + if (num < Integer.MIN_VALUE) return Integer.MIN_VALUE; + return num.intValue(); + } + + private static boolean isNumber(char c) { + String number = "0123456789"; + if (number.indexOf(c) == -1) { + return false; + } + return true; + } +} \ No newline at end of file diff --git a/Week_09/G20200343040202/LeetCode_917_0202.java b/Week_09/G20200343040202/LeetCode_917_0202.java new file mode 100644 index 00000000..0dd91a4a --- /dev/null +++ b/Week_09/G20200343040202/LeetCode_917_0202.java @@ -0,0 +1,18 @@ +class Solution { + public String reverseOnlyLetters(String S) { + Stack letters = new Stack(); + for (char c: S.toCharArray()) + if (Character.isLetter(c)) + letters.push(c); + + StringBuilder ans = new StringBuilder(); + for (char c: S.toCharArray()) { + if (Character.isLetter(c)) + ans.append(letters.pop()); + else + ans.append(c); + } + + return ans.toString(); + } +} \ No newline at end of file diff --git a/Week_09/G20200343040202/LeetCode_91_0202.java b/Week_09/G20200343040202/LeetCode_91_0202.java new file mode 100644 index 00000000..9cb9f904 --- /dev/null +++ b/Week_09/G20200343040202/LeetCode_91_0202.java @@ -0,0 +1,21 @@ +class Solution { + public int numDecodings(String s) { + + if (s == null || s.length() == 0) { + return 0; + } + int n = s.length(); + int[] dp = new int[n + 1]; + dp[0] = 1; + dp[1] = s.charAt(0) == '0' ? 0 : 1; + for (int i = 1; i < n; i++) { + char c = s.charAt(i); + char pre = s.charAt(i - 1); + dp[i + 1] = c == '0' ? 0 : dp[i]; + if (pre == '1' || (pre == '2' && c <= '6')) { + dp[i + 1] += dp[i - 1]; + } + } + return dp[n]; + } +} \ No newline at end of file