Write a function to find the longest common prefix string amongst an array of strings.
string longestCommonPrefix(vector<string> &strs) { // Start typing your C/C++ solution below // DO NOT write int main() function int row = strs.size(); if(row < 1) return ""; if(row == 1) return strs[0]; int strlength = strs[0].length(); for(int i = 1; i < row; ++i) if(strs[i].length() < strlength) strlength = strs[i].length(); int count = 0; bool flag = true; for(int i=0; i< strlength && flag; ++i) { char commonchar = strs[0][count]; int j = 0; for(; j < row; ++j) if(strs[j][count] != commonchar) {flag = false; break;} if((j == row) && (flag = true))count++; } return strs[0].substr(0,count); }
作者:doc_sgl 发表于2013-9-28 1:22:51 原文链接
阅读:157 评论:0 查看评论