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

思路:要去是寻找字符串vector里最长的公有前缀。

1。构造迭代器,遍历vector搜索最小string长度。然后从第一个字符开始进行遍历,如果遇到不一样的字符即返回,全部遍历完毕没问题就把它添加到前缀字符串里。

这样做效率比较差,有待改进。

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        unsigned minLen = 0xffffffff;
        unsigned i = ;
        string res = "";
        vector<string>::iterator it;
           for (it = strs.begin();it != strs.end(); it++)    {
               cout << (*it).size();
               minLen = ((*it).size() < minLen) ? ((*it).size()) : minLen;
           }
           // cout << "minlen is " << minLen << endl;
           if ((!minLen) || (minLen == 0xffffffff))
               return "";
           // cout << "minlen is " << minLen << endl;
           while (i != minLen)    {
               char temp;
               bool flag = false;
               it = strs.begin();
               temp = (*it)[i];
               for (it = strs.begin(); it != strs.end(); it++)    {
                   if (temp != (*it)[i])
                       return res;
               }
               res += temp;
               i ++;
           }
           return res;
    }
};

Longest common prefix | leetcode的更多相关文章

  1. Longest Common Prefix [LeetCode 14]

    1- 问题描述 Write a function to find the longest common prefix string amongst an array of strings. 2- 思路 ...

  2. Longest Common Prefix leetcode java

    题目: Write a function to find the longest common prefix string amongst an array of strings. 题解: 解题思路是 ...

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

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

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

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

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

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

  6. LeetCode专题-Python实现之第14题:Longest Common Prefix

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

  8. LeetCode题解(14)--Longest Common Prefix

    https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...

  9. 【LeetCode OJ 14】Longest Common Prefix

    题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...

随机推荐

  1. android模拟器经常出现的一些问题及解决办法

    1.Unable to get view server version from device exlipse下编写好android应用程序时候,右键项目 run as android applica ...

  2. [转]使用Beaglebone Black的SPI

    分类: Beaglebone Black2013-11-24 18:21 678人阅读 评论(6) 收藏 举报 beaglebone blackbeagleboneSPIdevice tree   目 ...

  3. Java代码安全测试解决方案

    Java代码安全测试解决方案: http://gdtesting.com/product.php?id=106

  4. iframe父子窗口取值

    父窗口中操作iframe:window.frames["iframeChild"].document //假如iframe的id为iframeChild 在子窗口中操作父窗口:wi ...

  5. MyFramework框架搭建(一)DAL层

    一直以来有一个想法,搭建一个属于自己的框架,将自己学到的东西整合到框架里,不断的完善,让它随着我的成长而成长,下面介绍我第一阶段的总结:DAL层搭建 一.基础配置 1.我用的是Ibatis.net框架 ...

  6. 安卓获取Assets目录下的资源

    获取Assets目录下的资源 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 ! ...

  7. JavaMail组件实现邮件功能

    实现邮件收发功能需要3个jar包: 1.JavaMail组件保内的mail.jar和smtp.jar包 2.JAF组件包里的activition.jar. 复制到WebRoot/WEB-INF/lib ...

  8. .net开发---自定义页面打印区域

    自定义页面打印区域 有3种办法: 办法一:将不需要打印的部位隐藏掉 Examp: <%-- (1)使用css样式,定义一个.noprint的class,将不打印的内容放入这个class内. -- ...

  9. MVC中,查询以异步呈现,分页不用异步的解决方案

    MVC中,查询以异步呈现,分页不用异步的解决方案 这种需求,用一个ASPX页面和一个ASCX分部视图就可以解决了,ASPX提供对ASCX的引用,ASCX显示列表信息,ASPX主页面提供查询功能 < ...

  10. 学习笔记_Java_day14—编码实战___一个注册页面的完整流程