标签:
Write a function to find the longest common prefix string amongst an array of strings.
class Solution { public: string longestCommonPrefix(vector<string>& strs) { int vSize = strs.size(); string result = ""; if(vSize == 0) return result; else if(vSize == 1) return strs[0]; result = strs[0]; int i,j; for(i = 1; i < vSize; i++){ for(j = 0; j < result.length() && j < strs[i].length(); j++){ if(strs[i][j] != result[j]) break; } result = result.substr(0,j); //截取字符串 } return result; } };
14.Longest Common Prefix (String)
标签:
原文地址:http://www.cnblogs.com/qionglouyuyu/p/4672304.html