LeetCode: Longest Common Prefix

来源:互联网 发布:java 列出n之前的质数 编辑:程序博客网 时间:2024/05/01 14:17
class Solution {
public:
    string longestCommonPrefix(vector<string> &strs) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        string ans = "";
        if(strs.size()==0)
            return ans;
        int i=0;
        while(strs[0][i]!='\0'){
            char p = strs[0][i];
            for(int j = 1;j<strs.size();j++){
                if(strs[j][i]!=p)
                    return ans;
            }
            ans += p;
            i++;
        }
        return ans;
    }
};
原创粉丝点击