首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
最长公共前缀
[编程题]最长公共前缀
热度指数:159173
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给你一个大小为 n 的字符串数组 strs ,其中包含n个字符串 , 编写一个函数来查找字符串数组中的最长公共前缀,返回这个公共前缀。
数据范围:
,
进阶:空间复杂度
,时间复杂度
示例1
输入
["abca","abc","abca","abc","abcc"]
输出
"abc"
示例2
输入
["abc"]
输出
"abc"
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(9)
邀请回答
收藏(668)
分享
提交结果有问题?
271个回答
228篇题解
开通博客
堆栈哲学
发表于 2021-07-14 17:51:59
精华题解
解法一:纵向扫描 将字符串数组看作一个二维空间,每一次从第一列开始。 确定所有字符子串中第一列字符。 逐层扫描后面每一列,遇到不同字符停止扫描。 图解: Java参考代码: import java.util.*; public class Solution { /**
展开全文
2019113913
发表于 2021-07-09 20:37:21
精华题解
题意思路: 题目给出长度为n的子串,找出子串中最长的公共前缀。 方法一:子串纵向查找 纵向遍历非常的直观,如下图所示,将每个字符串分别依次遍历每一列的元素,比较相同列上字符是否相同,若相同则比较下一个子串,若不同则最长公共前缀为上个遍历过的公共前缀。 复杂度分析:时间复杂度:O(mn),其中n 是字
展开全文
牛客题解官
发表于 2022-04-22 12:57:21
精华题解
题目主要信息: 给定一个字符串数组,其中有n个字符串,求所有字符串的最长公共前缀 公共前缀是指所有字符串都共有的前面部分的子串,从第一个字符开始 举一反三: 学会了本题的思路,你将可以解决类似的字符串问题: BM83. 字符串变形 BM85. 验证IP地址 方法:遍历查找(推荐使用) 思路: 既
展开全文
Maokt
发表于 2021-07-13 14:13:56
精华题解
算法思想一:横向扫描 解题思路: 用LCP(S1...Sn)表示字符串S1 ... Sn的最长公共前缀 可以得到结论:
展开全文
LifelongCode
发表于 2021-02-03 21:51:59
作者:LeetCode-Solution链接:https://leetcode-cn.com/problems/longest-common-prefix/solution/zui-chang-gong-gong-qian-zhui-by-leetcode-solution/ 解法1:对字符串数组进
展开全文
数据结构和算法
发表于 2021-04-03 21:17:40
1,解法一 先取第一个字符串当做他们的公共前缀 然后找出他和第2个字符串的公共前缀,然后再用这个找出的公共前缀分别和第3个,第4个……判断 public String longestCommonPrefix(String[] strs) { //边界条件判断
展开全文
已注销
发表于 2021-10-24 12:43:56
class Solution: def longestCommonPrefix(self , strs ): # write code here length = len(strs) if strs == '' or (length == 0)
展开全文
牛客983371096号
发表于 2021-11-20 21:24:35
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param strs string字符串一维数组 # @return string字符串 # class So
展开全文
张煌
发表于 2021-09-19 21:44:07
先数组排序,然后只需要比较最大和最小的暨第一个和最后一个string,保存一个标记位i,利用substr返回string中第一个到第i个组成的子字符串;注意,如果strs为空,必须返回"". class Solution { public: /** *
展开全文
加油做题
发表于 2022-06-10 19:59:29
char* longestCommonPrefix(char** strs, int strsLen ) { //如果字符串长度为0,则返回原字符串 if(strsLen==0){ return strs; } //初始化行列i,j int i
展开全文
超级码力233
发表于 2020-11-26 16:54:20
最长公共前缀 题目链接 Solution 求n个串的最长公共前缀。从0开始枚举答案,然后依次比较所有字符串的这一位,如果都相同,那么答案+1。注意一下答案最大是最小的字符串的长度。数据范围较小,暴力可过。 Code class Solution { public: string longes
展开全文
lee≤e
发表于 2022-04-17 21:22:20
#找出最长和最短的两个字符串比较这俩的前i位是否相同 class Solution: def longestCommonPrefix(self , strs: List[str]) -> str: # write code here if not str
展开全文
程序员学长
发表于 2021-10-30 23:40:45
最长公共前缀 问题描述 LeetCode14. 最长公共前缀 给你一个大小为 n 的字符串数组 strs ,其中包含n个字符串 , 编写一个函数来查找字符串数组中的最长公共前缀,返回这个公共前缀。 示例: 输入:["abca","abc","abca","abc","abcc"] 输出:"abc"
展开全文
go题解
发表于 2021-04-13 16:45:55
go解题答案 思路概括:纵向遍历 思路核心:1、外层遍历任意一个字符串,内层遍历每个字符串的第i个进行对比func longestCommonPrefix( strs []string ) string { if len(strs) == 0 { return "" } f
展开全文
问题信息
字符串
难度:
271条回答
668收藏
21949浏览
热门推荐
通过挑战的用户
查看代码
求乞者
2022-10-31 17:02:28
牛客17999...
2022-09-16 17:56:23
12:00
2022-09-16 17:19:08
院中的柿子树
2022-09-16 16:29:16
不停20190...
2022-09-16 16:13:24
相关试题
字符串分隔
字符串
评论
(3138)
dota2中,以下哪个英雄不具备隐...
游戏运营
评论
(1)
iPhone X状态条像素为多少?
360集团
iOS
iOS工程师
2019
评论
(1)
来自
360公司-2019校招...
iOS10以后UIScrollVi...
360集团
iOS
iOS工程师
2018
评论
(1)
来自
360公司-2018春招...
晶体管工作在放大区时,发射结电压和...
模拟电路
评论
(1)
最长公共前缀
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param strs string字符串一维数组 * @return string字符串 */ public String longestCommonPrefix (String[] strs) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param strs string字符串vector * @return string字符串 */ string longestCommonPrefix(vector
& strs) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param strs string字符串一维数组 # @return string字符串 # class Solution: def longestCommonPrefix(self , strs ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param strs string字符串一维数组 * @return string字符串 */ public string longestCommonPrefix (List
strs) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param strs string字符串一维数组 * @return string字符串 */ function longestCommonPrefix( strs ) { // write code here } module.exports = { longestCommonPrefix : longestCommonPrefix };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param strs string字符串一维数组 # @return string字符串 # class Solution: def longestCommonPrefix(self , strs: List[str]) -> str: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param strs string字符串一维数组 * @return string字符串 */ func longestCommonPrefix( strs []string ) string { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param strs string字符串一维数组 * @param strsLen int strs数组长度 * @return string字符串 */ char* longestCommonPrefix(char** strs, int strsLen ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param strs string字符串一维数组 # @return string字符串 # class Solution def longestCommonPrefix(strs) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param strs string字符串一维数组 * @return string字符串 */ def longestCommonPrefix(strs: Array[String]): String = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param strs string字符串一维数组 * @return string字符串 */ fun longestCommonPrefix(strs: Array
): String { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param strs string字符串一维数组 * @return string字符串 */ public String longestCommonPrefix (String[] strs) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param strs string字符串一维数组 * @return string字符串 */ export function longestCommonPrefix(strs: string[]): string { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param strs string字符串一维数组 * @return string字符串 */ func longestCommonPrefix ( _ strs: [String]) -> String { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param strs string字符串一维数组 * @return string字符串 */ pub fn longestCommonPrefix(&self, strs: Vec
) -> String { // write code here } }
["abca","abc","abca","abc","abcc"]
"abc"
["abc"]
"abc"