https://leetcode.com/problems/longest-common-prefix/

原题:

Write a function to find the longest common prefix string amongst an array of strings.

思路:

简单,直接遍历查找即可。

AC代码:

 class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int n=strs.size();
if (n==)
return strs[];
if (n==)
return "";
int k=strs[].size();
bool flag=true;
string res="";
for(int i=;i<n;i++)
k=(k<strs[i].size())? k:strs[i].size();
for(int t=;t<k;t++){
for(int i=;i<n;i++){
if(strs[i][t]!=strs[][t]){
flag=false;
break;
}
}
if (flag==true)
res.push_back(strs[][t]);
else
break;
}
return res;
}
};

LeetCode题解(14)--Longest Common Prefix的更多相关文章

  1. 《LeetBook》leetcode题解(14):Longest Common Prefix[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  3. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  4. 【一天一道LeetCode】#14 Longest Common Prefix

    一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...

  5. C# 写 LeetCode easy #14 Longest Common Prefix

    14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  6. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

  7. 【LeetCode】14 - Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...

  8. 【LeetCode】14. Longest Common Prefix (2 solutions)

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  9. Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)

    1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...

随机推荐

  1. Laya List翻页滚动方案 & List滚动源码解析

    Laya List翻页滚动方案 & List滚动源码解析 @author ixenos 2019-03-29 1.List翻页滚动方案 /** * 计算下一页的起始索引, 不足时补足 * @p ...

  2. hdu6061[NTT推公式] 2017多校3

    /*hdu6061[NTT推公式] 2017多校3*/ #include <bits/stdc++.h> using namespace std; typedef long long LL ...

  3. BZOJ3532 [Sdoi2014]Lis 【网络流退流】

    题目 给定序列A,序列中的每一项Ai有删除代价Bi和附加属性Ci.请删除若 干项,使得4的最长上升子序列长度减少至少1,且付出的代价之和最小,并输出方案. 如果有多种方案,请输出将删去项的附加属性排序 ...

  4. VS的一些错误解决方法记录

    1.errorC2664 "bool CMarkup::AddElem(MCD_CSTR,MCD_CSTR,int)":不能将参数1从“constchar [7]” 转换位&quo ...

  5. ubuntu mysql安装及需要其他主机连服务器mysql时的设置(error:10061)

    说明: 一个朋友在使用ubuntu-server 16.04安装mysql,设置远程访问的时候出现了问题,请我帮忙.但是,我也没有使用过ubuntu安装mysql,于是乎搜索了很多技术文件,比着葫芦画 ...

  6. 洛谷 [P1436] 反素数

    算术基本定理的应用 算术基本定理: 一个正整数 \(N\) 能唯一分解成如下形式 \[N=p_1 ^ {c_1}p_2^{c_2}\cdots P_m ^ {c_m}\] 其中 \(c_i\) 都是正 ...

  7. 洛谷 [P2734] 游戏

    博弈论+区间dp 有博弈论吗?大约只有一个博弈论的壳子 设 dp[i][j] 表示区间 i ~ j 先手最多能取多少, 它可以由 i ~ j - 1 与 i + 1 ~ j 来转移, 等于上述两个区间 ...

  8. 标准C程序设计七---26

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  9. 如何新建.htaccess文件

    如何新建.htaccess文件 (1)新建文本文档,即txt文件: (2)把重写代码写进去: (3)然后选择[另存为],命名为.htaccess即可.

  10. html5对各浏览器的支持情况

    考虑到HTML5标准的制定原则:新特性基于HTML.CSS.DOM 以及 JavaScript:减少对外部插件的需求(比如Flash):独立于设备等,我们选取了HTML 5的几项主要特性来评价浏览器系 ...