标签:style blog io ar color sp for on div
class Solution { public: string longestCommonPrefix(vector<string> &strs) { int ind, tail, len, tmp; len = strs.size(); if(len == 0) return ""; if(len == 1) return strs[0]; ind = 0; for(int i=1;i<len;i++){ if(strs[i].length() < strs[ind].length()){ ind = i; } } tmp = tail = strs[ind].length() - 1; for(int i=0;i<len;i++){ int k=0; while(k<=tmp){ if(strs[i][k] == strs[ind][k]){ k++; } else break; } tmp = k-1; } return strs[ind].substr(0, tmp+1); } };
Write a function to find the longest common prefix string amongst an array of strings.
标签:style blog io ar color sp for on div
原文地址:http://www.cnblogs.com/code-swan/p/4141311.html