Longest Common Prefix

class Solution {
public:
    string longestCommonPrefix(vector<string> &strs) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int size = strs.size();
        if(size == 0)
        return "";
        if(size == 1)
        return strs[0];
        int i = 0;
        string result = "";
        while(1)
        {
            if(strs[0][i] == '\0')
            break;
            char temp = strs[0][i];
            int j;
            for( j = 1; j < size;j++)
            {
                if(temp != strs[j][i])
                break;
            }
            if(j!=size)
            break;
            i++;
            result+=temp;
        }
        return result;
    }
};

posted on 2013-09-06 20:27  邪灵天使  阅读(107)  评论(0编辑  收藏  举报