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

思路:找最长公共前缀 常规方法

string longestCommonPrefix(vector<string> &strs) {
if(strs.size() == ) return "";
if(strs.size() == ) return strs[];
string ans;
int n = ;
while()
{
for(int i = ; i < strs.size(); i++)
{
if(strs[i].size() <= n || strs[i - ].size() <= n || strs[i][n] != strs[i - ][n]) //如果n超出了字符串长度 或对应位置不等 返回答案
{
return ans;
}
}
ans += strs[].substr(n, );
n++;
}
}

【leetcode】Longest Common Prefix (easy)的更多相关文章

  1. 【leetcode】Longest Common Prefix

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

  2. 【LeetCode】 Longest Common Prefix

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

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

    这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...

  4. 【leetcode】Reverse Linked List(easy)

    Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...

  5. 【leetcode】Count and Say (easy)

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  6. 【leetcode】Factorial Trailing Zeroes(easy)

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  7. Leetcode 之Longest Common Prefix(34)

    这题实现起来还是挺麻烦的,就偷懒使用下库函数strtod().第二个参数表示字符中不是数字的地方,如果后面是空格,则认为其仍是数字,否则不是. bool isNumber(char *s) { cha ...

  8. Leetcode 之Longest Common Prefix(33)

    在一组字符串中找到最长的子串.采用纵向匹配,遇到第一个不匹配的停止. string longestComPrefix(vector<string> &strs) { if (str ...

  9. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

随机推荐

  1. Html.ActionLink 几种重载方式说明及例子

    本文整理了该方法的几种重载形式:一 Html.ActionLink("linkText","actionName")该重载的第一个参数是该链接要显示的文字,第二 ...

  2. OpenGL5-纹理贴图

    代码下载 #include "CELLWinApp.hpp"#include <gl/GLU.h>#include <assert.h>#include & ...

  3. (转)实战Memcached缓存系统(6)Memcached CAS的多线程程序实例

    1. 源程序 package com.sinosuperman.memcached; import java.io.IOException; import java.net.InetSocketAdd ...

  4. JavaScript、jQuery、HTML5、Node.js实例大全-读书笔记2

    技术很多,例子很多,只好慢慢学,慢慢实践!!现在学的这本书是[JavaScript实战----JavaScript.jQuery.HTML5.Node.js实例大全] JavaScript.jQuer ...

  5. [设计模式]Netd中的命令设计模式

    命令模式 有如下的角色: (1)调用者(invoker) (2)命令接收者(receiver) (3)客户端(client) (4)命令对象(command) public interface Com ...

  6. 在ThinkPHP3.x框架中实现将原创文章第一时间推送到百度收录

    前两天自己写的一篇文章“针对BootStrap中tabs控件的美化和完善”被别的网站给转载了,这也许是值得高兴的一件事情,但是有些网站并没有注明来源和作者.而去百度搜索这篇文章,排名第一的居然是那些转 ...

  7. PHP实现中文简体字和繁体字互转

    function convert($str, $action='S'){ if($action != 'S' && $action != 'T'){ return $str; } $s ...

  8. Speech Patterns (string)

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  9. Cadence封装制作之表贴封装的制作

    以0805封装为例 1.打开PCB editor-> Allegro PCB Design XL 2.File -> New ① Drawing Type -> Package Sy ...

  10. Google code: Why ‘Everything up-to-date’ when pushing (git)

    原文链接:http://blog.rexzhao.com/2011/11/28/google-code-git-everything-up-to-date-when-push.html 第一次在Goo ...