题解 | #最长公共前缀#
最长公共前缀
https://www.nowcoder.com/practice/28eb3175488f4434a4a6207f6f484f47
class Solution { public: /** * * @param strs string字符串vector * @return string字符串 */ string longestCommonPrefix(vector<string>& strs) { // write code here if(strs.size() == 0)return ""; string ans; int i = 0; for(int i = 0; i < strs[0].size(); i++){ char temp = strs[0][i]; for(int j = 1; j < strs.size(); j++){ if(strs[j].size() <= i){ return strs[1].substr(0, i); } if(strs[j][i] != temp){ return strs[1].substr(0, i); } } } return strs[0]; } };