From 6074dbc74617647916bb07ecf9ebece1a05acd82 Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 6 Sep 2023 11:35:24 +0800 Subject: [PATCH 01/14] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20.gitignore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 93d3622..880e784 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,4 @@ hs_err_pid* replay_pid* >>>>>>> gitea/master +.idea/misc.xml From 82f0fcc086479a4bb0a6a357d7590a346824a87e Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 6 Sep 2023 11:37:27 +0800 Subject: [PATCH 02/14] =?UTF-8?q?=E5=88=A0=E9=99=A4=20misc.xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .idea/misc.xml diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index e0844bc..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From c20b688bba1bd0664b74f4322e874a5f9f81c41d Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 6 Sep 2023 11:37:31 +0800 Subject: [PATCH 03/14] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20workspace.xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1cb0ea7..9f2e057 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -5,10 +5,7 @@ - - - - + - - - - - file://$PROJECT_DIR$/src/solution/Solution.java - 105 - - - file://$PROJECT_DIR$/src/solution/Solution.java - 83 - - - - \ No newline at end of file From ead209eba8c1021ceba495e81cbe03cd04debaac Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 6 Sep 2023 11:38:21 +0800 Subject: [PATCH 04/14] =?UTF-8?q?=E5=88=A0=E9=99=A4=20modules.xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/modules.xml | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .idea/modules.xml diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 56749e5..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file From 5bd8197e7887a27fdbe17c46368f4b40b4b8fe6f Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 6 Sep 2023 11:38:38 +0800 Subject: [PATCH 05/14] =?UTF-8?q?=E5=88=A0=E9=99=A4=20vcs.xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/vcs.xml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 263e8e0654e6adbdff62d77f3b82385f3ce0d537 Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 6 Sep 2023 11:38:47 +0800 Subject: [PATCH 06/14] =?UTF-8?q?=E5=88=A0=E9=99=A4=20workspace.xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 91 --------------------------------------------- 1 file changed, 91 deletions(-) delete mode 100644 .idea/workspace.xml diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 9f2e057..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,91 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1693822719192 - - - - - - \ No newline at end of file From 8eb127378fde3c265eb6415b4b19dbefc6c29f4a Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 6 Sep 2023 11:41:30 +0800 Subject: [PATCH 07/14] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20.gitignore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 880e784..c4b67c0 100644 --- a/.gitignore +++ b/.gitignore @@ -51,6 +51,7 @@ bin/ *.tar.gz *.rar .idea/* +Solution.iml # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* From e20fbc3713785c842ac384ca44e26734b3fa5f6e Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 6 Sep 2023 11:41:42 +0800 Subject: [PATCH 08/14] =?UTF-8?q?=E5=88=A0=E9=99=A4=20Solution.iml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Solution.iml | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 Solution.iml diff --git a/Solution.iml b/Solution.iml deleted file mode 100644 index c90834f..0000000 --- a/Solution.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file From ec10ce6c8ab5fee635cda7d59dda185bf6e4a147 Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 6 Sep 2023 12:00:57 +0800 Subject: [PATCH 09/14] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Solution.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/solution/Solution.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/solution/Solution.java b/src/solution/Solution.java index 66ccc6b..8853da8 100644 --- a/src/solution/Solution.java +++ b/src/solution/Solution.java @@ -105,4 +105,24 @@ public class Solution { String result = Arrays.stream(nums1).mapToObj(String::valueOf).collect(Collectors.joining(",", "[", "]")); System.out.println(result); } + + public static String longestCommonPrefix(String[] strs) { + // ["flower","flow","flight"] + int maxPrefix = 0; + int index = 0; + StringBuilder temp = new StringBuilder(); + if (strs != null){ + for (int i = 0; i < strs.length; i++) { + if (strs[i] != ""){ + for (int j = 0; j < strs[i].length() && j < index; j++) { + temp.append(strs[i].charAt(index)); + break; + } + } else { + return ""; + } + } + } + return ""; + } } From 755fdc488a1b6d6a60cbeba4822a6663ac2134dd Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 6 Sep 2023 12:01:01 +0800 Subject: [PATCH 10/14] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20test.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/solution/test.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/solution/test.java b/src/solution/test.java index c327824..46577af 100644 --- a/src/solution/test.java +++ b/src/solution/test.java @@ -8,14 +8,18 @@ import java.util.stream.Collectors; public class test { public static void main(String[] args) { - int[] nums1 = new int[]{1,2,3,0,0,0}; - int m = 3; - int[] nums2 = new int[]{2,5,6}; - int n = 3; +// int[] nums1 = new int[]{1,2,3,0,0,0}; +// int m = 3; +// int[] nums2 = new int[]{2,5,6}; +// int n = 3; // int[] nums1 = new int[]{1}; // int m = 1; // int[] nums2 = new int[]{}; // int n = 0; - Solution.merge(nums1,m,nums2,n); +// Solution.merge(nums1,m,nums2,n); + String[] strs = new String[]{"flower","flow","flight"}; + String result = Solution.longestCommonPrefix(strs); + System.out.println(result); } + } From e57ebeadf8919a485b00d76ae4348ce86db2c422 Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 6 Sep 2023 14:28:53 +0800 Subject: [PATCH 11/14] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20test.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/solution/test.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/solution/test.java b/src/solution/test.java index 46577af..29539f2 100644 --- a/src/solution/test.java +++ b/src/solution/test.java @@ -17,7 +17,9 @@ public class test { // int[] nums2 = new int[]{}; // int n = 0; // Solution.merge(nums1,m,nums2,n); - String[] strs = new String[]{"flower","flow","flight"}; +// String[] strs = new String[]{"flower","flow","flight"}; + String[] strs = new String[]{"abcg","abcecar","abr"}; + String result = Solution.longestCommonPrefix(strs); System.out.println(result); } From 8ce22bbbbbc52f16668a0b16e998d645e9c681f6 Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 6 Sep 2023 14:28:56 +0800 Subject: [PATCH 12/14] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Solution.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/solution/Solution.java | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/solution/Solution.java b/src/solution/Solution.java index 8853da8..89dabdd 100644 --- a/src/solution/Solution.java +++ b/src/solution/Solution.java @@ -112,16 +112,26 @@ public class Solution { int index = 0; StringBuilder temp = new StringBuilder(); if (strs != null){ - for (int i = 0; i < strs.length; i++) { - if (strs[i] != ""){ - for (int j = 0; j < strs[i].length() && j < index; j++) { - temp.append(strs[i].charAt(index)); - break; + for (int k = 0; k < strs[0].length(); k++) { + char indexChar = strs[0].charAt(index); + for (int i = 1; i < strs.length; i++) { + if (strs[i] != ""){ + for (int j = 0; j < 1; j++) { + char crrChar = strs[i].charAt(index); + if (indexChar != crrChar){ + return String.valueOf(temp); + } + if (i == strs.length- 1){ + temp.append(crrChar); + index++; + } + } + } else { + return ""; } - } else { - return ""; } } + } return ""; } From 654ae8766831f7c54174f8932a918f71327c06bb Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 6 Sep 2023 15:06:23 +0800 Subject: [PATCH 13/14] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20test.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/solution/test.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/solution/test.java b/src/solution/test.java index 29539f2..153d551 100644 --- a/src/solution/test.java +++ b/src/solution/test.java @@ -18,7 +18,7 @@ public class test { // int n = 0; // Solution.merge(nums1,m,nums2,n); // String[] strs = new String[]{"flower","flow","flight"}; - String[] strs = new String[]{"abcg","abcecar","abr"}; + String[] strs = new String[]{"flower","flower","flower","flower"}; String result = Solution.longestCommonPrefix(strs); System.out.println(result); From a83429a6b794491f269fccaa74b710ca4abc514b Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Wed, 6 Sep 2023 15:06:26 +0800 Subject: [PATCH 14/14] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Solution.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/solution/Solution.java | 70 +++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/src/solution/Solution.java b/src/solution/Solution.java index 89dabdd..d09cd89 100644 --- a/src/solution/Solution.java +++ b/src/solution/Solution.java @@ -106,33 +106,55 @@ public class Solution { System.out.println(result); } +// public static String longestCommonPrefix(String[] strs) { +// int index = 0; +// StringBuilder temp = new StringBuilder(); +// if (strs != null){ +// for (int k = 0; k < strs[0].length(); k++) { +// char indexChar = strs[0].charAt(index); +// if (strs.length == 1){ +// return strs[0]; +// } +// for (int i = 1; i < strs.length; i++) { +// if (strs[i] != ""){ +// for (int j = 0; j < 1; j++) { +// if (index >= strs[i].length()){ +// return String.valueOf(temp); +// } +// char crrChar = strs[i].charAt(index); +// if (indexChar != crrChar){ +// return String.valueOf(temp); +// } +// if (i == strs.length- 1){ +// temp.append(crrChar); +// index++; +// } +// } +// } else { +// return ""; +// } +// } +// } +// return String.valueOf(temp); +// } +// return ""; +// } + public static String longestCommonPrefix(String[] strs) { - // ["flower","flow","flight"] - int maxPrefix = 0; - int index = 0; - StringBuilder temp = new StringBuilder(); - if (strs != null){ - for (int k = 0; k < strs[0].length(); k++) { - char indexChar = strs[0].charAt(index); - for (int i = 1; i < strs.length; i++) { - if (strs[i] != ""){ - for (int j = 0; j < 1; j++) { - char crrChar = strs[i].charAt(index); - if (indexChar != crrChar){ - return String.valueOf(temp); - } - if (i == strs.length- 1){ - temp.append(crrChar); - index++; - } - } - } else { - return ""; - } + if (strs == null || strs.length == 0) { + return ""; + } + int length = strs[0].length(); + int count = strs.length; + for (int i = 0; i < length; i++) { + char c = strs[0].charAt(i); + for (int j = 1; j < count; j++) { + if (i == strs[j].length() || strs[j].charAt(i) != c) { + return strs[0].substring(0, i); } } - } - return ""; + return strs[0]; } + }