leetcode-longest common prefix

class Solution {
public:
   
    string longestCommonPrefix(vector<string> &strs) 
    {
        int size = strs.size();
        if(size==0)
            return "";
        if(size==1)
            return strs[0];
        string result = "";
        int i,j;
        for(j=0;;j++)
        {
            for(i=0;i<size-1;i++)
            {
                if(strs[i].length()<=j) break;
                if(strs[i][j]==strs[i+1][j])
                    continue;
                else
                    break;
            }
            if(i!=size-1)
                break;
            else
                result += strs[i][j];
        }
        return result;
    }
};

posted @ 2014-06-04 15:04  海滨银枪小霸王  阅读(108)  评论(0)    收藏  举报