2017-09-09 LeetCode_014 Longest Common Prefix

来源:互联网 发布:vb picture 图形放大 编辑:程序博客网 时间:2024/06/05 20:17

14. Longest Common Prefix

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

solution:

class Solution {
2
public:
3
    string longestCommonPrefix(vector<string>& strs) {
4
        if (strs.size() == 0) return "";
5
        string s(strs[0]);
6
        for (int i = 0; i < strs.size(); i++) {
7
            string temp;
8
            for (int j = 0; j < s.length() && j < strs[i].length(); j++)
9
                if (s[j] == strs[i][j]) temp += s[j];
10
                else break;
11
            s = temp;
12
        }
13
        return s;
14
    }
15
};





原创粉丝点击