您的位置:首页 > 其它

LeetCode:Longest Common Prefix

2016-01-10 17:13 302 查看


Longest Common Prefix

Total Accepted: 80786 Total
Submissions: 300453 Difficulty: Easy

Write a function to find the longest common prefix string amongst an array of strings.

Subscribe to see which companies asked this question

Hide Tags
String

code:

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        int strSize = strs.size();
        if(0 == strSize) return "";
        
        for(int i=0;i<strs[0].size();i++) {
            char c = strs[0][i];
            for(int j=0;j<strs.size();j++)
                if(i==strs[j].size() + 1 || c != strs[j][i]) return strs[0].substr(0, i);
        }
        return strs[0];
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: