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] =?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 ""; + } }