LeetCode-14

xiaoxiao2021-02-28  16

class Solution { public: string longestCommonPrefix(vector<string>& strs) { string s = ""; if(strs.size() == 0) return ""; for(int i=0; i<strs[0].length(); i++) { char c = strs[0].at(i); for(int j=1; j<strs.size(); j++) { if(i>=strs[j].size() || strs[j].at(i)!=c) return s; } s += c; } return s; } };用第一个字符串和剩余的字符串进行从第一个字符开始的对比,用两个循环找出最大前缀字符串。
转载请注明原文地址: https://www.6miu.com/read-2630527.html

最新回复(0)