当前位置: 首页> 教育> 高考 > 黄骅港口_ur电商seo平台价值_国外搜索引擎网址_附子seo教程

黄骅港口_ur电商seo平台价值_国外搜索引擎网址_附子seo教程

时间:2025/8/12 2:14:51来源:https://blog.csdn.net/Prince140678/article/details/142792230 浏览次数:0次
黄骅港口_ur电商seo平台价值_国外搜索引擎网址_附子seo教程

题目出处

14-最长公共前缀-题目出处

题目描述

在这里插入图片描述

个人解法

思路:

todo

代码示例:(Java)

todo

复杂度分析

todo

官方解法

14-最长公共前缀-官方题解

方法1:横向扫描

思路:

在这里插入图片描述

代码示例:(Java)

public String longestCommonPrefix(String[] strs) {if (strs == null || strs.length == 0) {return "";}String prefix = strs[0];int count = strs.length;for (int i = 1; i < count; i++) {prefix = longestCommonPrefix(prefix, strs[i]);if (prefix.length() == 0) {break;}}return prefix;}public String longestCommonPrefix(String str1, String str2) {int length = Math.min(str1.length(), str2.length());int index = 0;while (index < length && str1.charAt(index) == str2.charAt(index)) {index++;}return str1.substring(0, index);}

复杂度分析

  • 时间复杂度:O(mn),其中 m 是字符串数组中的字符串的平均长度,n 是字符串的数量。最坏情况下,字符串数组中的每个字符串的每个字符都会被比较一次。
  • 空间复杂度:O(1)。使用的额外空间复杂度为常数。

方法2:纵向扫描

思路:

在这里插入图片描述

代码示例:(Java)

public String longestCommonPrefix(String[] strs) {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 strs[0];}

复杂度分析

  • 时间复杂度:O(mn),其中 m 是字符串数组中的字符串的平均长度,n 是字符串的数量。最坏情况下,字符串数组中的每个字符串的每个字符都会被比较一次。
  • 空间复杂度:O(1)。使用的额外空间复杂度为常数。

方法3:分治

思路:

在这里插入图片描述

代码示例:(Java)

public String longestCommonPrefix(String[] strs) {if (strs == null || strs.length == 0) {return "";} else {return longestCommonPrefix(strs, 0, strs.length - 1);}}public String longestCommonPrefix(String[] strs, int start, int end) {if (start == end) {return strs[start];} else {int mid = (end - start) / 2 + start;String lcpLeft = longestCommonPrefix(strs, start, mid);String lcpRight = longestCommonPrefix(strs, mid + 1, end);return commonPrefix(lcpLeft, lcpRight);}}public String commonPrefix(String lcpLeft, String lcpRight) {int minLength = Math.min(lcpLeft.length(), lcpRight.length());for (int i = 0; i < minLength; i++) {if (lcpLeft.charAt(i) != lcpRight.charAt(i)) {return lcpLeft.substring(0, i);}}return lcpLeft.substring(0, minLength);}

复杂度分析

在这里插入图片描述

方法4:二分查找

思路:

在这里插入图片描述

代码示例:(Java)

public String longestCommonPrefix(String[] strs) {if (strs == null || strs.length == 0) {return "";}int minLength = Integer.MAX_VALUE;for (String str : strs) {minLength = Math.min(minLength, str.length());}int low = 0, high = minLength;while (low < high) {int mid = (high - low + 1) / 2 + low;if (isCommonPrefix(strs, mid)) {low = mid;} else {high = mid - 1;}}return strs[0].substring(0, low);}public boolean isCommonPrefix(String[] strs, int length) {String str0 = strs[0].substring(0, length);int count = strs.length;for (int i = 1; i < count; i++) {String str = strs[i];for (int j = 0; j < length; j++) {if (str0.charAt(j) != str.charAt(j)) {return false;}}}return true;}

复杂度分析

在这里插入图片描述

考察知识点

收获

Gitee源码位置

14-最长公共前缀-源码

同名文章,已同步发表于CSDN,个人网站,公众号

  • CSDN

    工一木子
  • 个人网站

    工藤新一
  • 公众号

    在这里插入图片描述
关键字:黄骅港口_ur电商seo平台价值_国外搜索引擎网址_附子seo教程

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: