【LeetCode】14 - Longest Common Prefix

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

Solution:

 1 class Solution {
 2 public:
 3     string longestCommonPrefix(vector<string>& strs) {      //runtime:4ms
 4         string str="";
 5         if(strs.empty())return str;
 6         int index=0;
 7
 8         while(true){
 9             if(index>=strs[0].size())return str;
10             char c=strs[0][index];
11             for(int i=1;i<strs.size();i++){
12                 if(index>=strs[i].size()||strs[i][index]!=c)return str;
13             }
14             str+=c;
15             index++;
16         }
17
18     }
19 };
时间: 2024-08-16 20:58:36

【LeetCode】14 - Longest Common Prefix的相关文章

【LeetCode】014 Longest Common Prefix

题目:LeetCode 014 Longest Common Prefix 题意:给出一组字符串求公共前缀 思路:很多个字符串的公共前缀应该不会很高,所以直接暴力解决就好 但是又有个特判,即当只有一个字符串的时候,直接返回即可.另外,一定要注意每次利用下标访问字符串的时候,一定要判断是否在有效范围内. 代码如下: 1 class Solution { 2 public: 3 string longestCommonPrefix(vector<string>& strs) { 4 int

[LeetCode][Python]14: Longest Common Prefix

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/longest-common-prefix/14: Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings.===Comments by Dabay===注意边

【leetcode】1143. Longest Common Subsequence

题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order

14. Longest Common Prefix【leetcode】

14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 寻找一个数组中最长的公共前缀 例如["baaa","caaabbb","aaaa"]输出"aaa" 结题思路: 判断非空的情况在进行计算 取第一个字符串最为标杆进行对比,因为最终结果一定在第一位中 用第一

leetCode 14. Longest Common Prefix 字符串

14. Longest Common Prefix 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.size() == 0)

leetcode -- 刷效率 Longest Common Prefix

题目描述: Write a function to find the longest common prefix string amongst an array of strings. 很简单的一道题目,但是我写了3个不一样的版本,运行时间确实数倍之差..贴代码如下: 版本1: 这个版本的运行时间为  44 ms 版本2: 这个版本的运行时间为  16 ms 两者之间的区别便是:有无创建string ret返回字符串..  [updated]版本3:使用字符数组,时间同样是16ms,代码如下:

No.14 Longest Common Prefix

No.14 Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 求一组string的最长公共前缀想法:找到最短的那个,然后依次对比 典型的字符串操作,从前向后比较即可 1 #include "stdafx.h" 2 #include <string> 3 #include <vector> 4 #i

leedCode练题——14. Longest Common Prefix

1.题目 14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: ["flower","flow","flight&qu

LeetCode记录之14——Longest Common Prefix

本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest common prefix string amongst an array of strings. 编写一个函数来查找字符串数组中最长的公共前缀字符串. 1 class Solution { 2 public String longestCommonPrefix(String[] strs) { 3