LeetCode14. Longest Common Prefix

来源:互联网 发布:恋夜秀场破解软件酷安 编辑:程序博客网 时间:2024/06/07 06:10

LeetCode14. 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) {return "";}int j;string prefix = strs[0];for (int i = 1; i < strs.size(); i++) {for (j = 0; j < prefix.size() && j < strs[i].size(); j++) {if (prefix[j] != strs[i][j]) {break;}}prefix = prefix.substr(0, j);}return prefix;}};