Write a function to find the longest common prefix string amongst an array of strings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Solution { public: string longestCommonPrefix(vector<string>& strs) { if(strs.size() == 0) return ""; string s = strs[0]; for(int i = 1; i < strs.size(); i++) { for(int j = 0; j < s.length(); j++) { if(s[j] != strs[i][j]) { s = s.substr(0, j); break; } } } return s; } }; |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼