leetcode六月每日一題 leetcode14

在這裏插入圖片描述
暴力解法~

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if(strs.empty()) return "";
        string res="";
        for(int i=0;i<strs[0].size();i++){
            for(int j=1;j<strs.size();j++){
                if(i==strs[j].length()||strs[0][i]!=strs[j][i]){
                    return res;
                }
            }
            res+=strs[0][i];
        }
        return res;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章