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. 剑指offer_面试题11 数值的整数次方_考察代码的完整性

    测试通过代码: package t0825; public class Power { public static void main(String[] args){ System.out.print ...

  2. Visual Studio下SQLite数据库开发环境设置

    由于我们介绍的内容都是基于微软的Visual Studio下开发的Win32平台,所以下边我们介绍Visual Studio下SQLite数据库开发环境设置.具体而言我们有两种方式可以在Visual ...

  3. block回调具体例子

    //main.m ////  main.m//  回调////  Created by hehe on 15/9/10.//  Copyright (c) 2015年 wang.hehe. All r ...

  4. javascript笔记——密码组合规则

    //6-16个字符,字母加数字或符号的组合密码[必须全部包含] var filter = /^(?=.*[0-9])(?=.*[a-z])(?=.*[!@#$%^&*])(?=.*[A-Z]) ...

  5. C++ typedef用法小结

    一.typedef的四个用法 用法一: 为复杂的声明定义一个新的简单的别名.方法是:在原来的声明里逐步用别名替换一部分复杂声明,如此循环,把带变量名的部分留到最后替换,得到的就是原声明的最简化版.举例 ...

  6. java web中cookie的永久创建与撤销

    一.首先是创建cookie 当在数据库中查找知道所输入的用户名和密码正确之后,就开始创建: String cb=request.getParameter("cb");//cb就是登 ...

  7. Poj 3239 Solution to the n Queens Puzzle

    1.Link: http://poj.org/problem?id=3239 2.Content: Solution to the n Queens Puzzle Time Limit: 1000MS ...

  8. PHP 测试程序运行时间 microtime函数用法

    PHP microtime() 函数PHP Date / Time 函数定义和用法microtime() 函数返回当前 Unix 时间戳和微秒数.语法microtime(get_as_float)参数 ...

  9. Check Big/Little Endian

    Little endian:Low memory address stores low byte value.(eg.  short int 0x2211   0xbfd05c0e->0x11 ...

  10. Spring Cloud OAuth

    In this blog post we will create a secure API for external access, using OAuth 2.0, to the microserv ...