[LeetCode]14. Longest Common Prefix

来源:互联网 发布:上海华讯网络上市了吗 编辑:程序博客网 时间:2024/06/11 17:41

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

寻找一组字符串中的最大公共字符串

#include<iostream>#include<string>#include<vector>using namespace std;class Solution {public:string longestCommonPrefix(vector<string> &strs) {int n = strs.size();string res;if (n == 0)return res;for (int pos = 0; pos < strs[0].size(); pos++)//最长前缀的长度不超过strs[0].size(),逐个字符的比较{for (int k = 1; k < n; k++)//strs[0]的第pos个字符分别和strs[1...n-1]的第pos个字符比较{if (strs[k].size() == pos || strs[k][pos] != strs[0][pos])return res;}res.push_back(strs[0][pos]);}return res;}};



0 0