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

class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
if(strs.empty()) return "";
int minLength = strs[].length();
for(int i = ; i < strs.size(); ++ i) minLength = min(minLength,(int)strs[i].length());
bool flag = false;
int i = ;
for(i = ; i < minLength; ++ i){
char ch = strs[][i];
for(int j = ; j < strs.size(); ++ j){
if(strs[j][i]!=ch){flag=true;break;}
}
if(flag) break;
}
return strs[].substr(,i);
}
};

Leetcode Longest Common Prefix的更多相关文章

  1. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  2. Leetcode::Longest Common Prefix && Search for a Range

    一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...

  3. LeetCode: Longest Common Prefix 解题报告

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

  4. [LeetCode] Longest Common Prefix 字符串公有前序

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

  5. LeetCode——Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 写一个函数找出字符串数组中 ...

  6. [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀

    题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...

  7. LeetCode Longest Common Prefix 最长公共前缀

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

  8. leetcode Longest Common Prefix 多个字符串的最长字串

    public class Solution { public String get(String a,String b) { if(a==""||b=="") ...

  9. leetcode Longest Common Prefix python

    class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str ...

随机推荐

  1. Google 地图 API V3 之事件

    Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...

  2. Debian 8安装中文字体

    1.使用的镜像是debian-8.3.0-amd64-kde-CD-1.iso,下载链接可在Debian网站找到,系统安装完成后中文显示为方框 2.安装字体 apt-get install xfont ...

  3. 1 web.xml配置详解

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  4. Android之layout_gravity与gravity解析

    相信layout_gravity和gravity这两个属性一直困扰着很多人,很多初学者都分不清这两个属性有什么区别,以及怎样区分它们.它们中,有一个表示的是一个控件在父布局中的位置,而另一个表示的是一 ...

  5. Java重点识记

    1.final修饰的类不能被继承,已经达到类层次中的最低层. 2.abstract修饰的类或成员方法,表明是抽象的,含有抽象方法的类必须说明是抽象类,必须派生出子类. 3.JAVA对逻辑与和逻辑或提供 ...

  6. PHP中的变量与常量详解

    几乎所有的编程语言都会涉及到变量和常量这两个概念,PHP也不例外.本节将介绍PHP语言中的变量和常量的应用方法. 一.什么是变量和常量 在程序执行的过程中,变量存储的值可以随时改变,而常量存储的值是不 ...

  7. 在Linux上挂载Windows共享文件夹,如何开机自动挂载(mount)?

    按照一般的思路,我们先将文件夹挂载上去,命令如下: mkdir /mnt/share_software mount //192.9.206.43/share_software /mnt/share_s ...

  8. 限制帐号同时两处以上登录-ASP.NET

    ///登录页面 Hashtable haol = (Hashtable)Application["olTable"]; if (haol == null) { haol = new ...

  9. jquery_DOM笔记

    回头补充知识: jquery事件复习: bind() 用于绑定多个事件,当某一个节点需要进行多项处理的时候使用 .使用方式 $(select).bind({event:function(),event ...

  10. TCP/IP四层模型

    转自:http://www.cnblogs.com/BlueTzar/articles/811160.html ISO制定的OSI参考模型的过于庞大.复杂招致了许多批评.与此对照,由技术人员自己开发的 ...