14. Longest Common Prefix

2017-06-14  本文已影响0人  YellowLayne

1.描述

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

2.分析

3.代码

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if (strs.size() <= 0) return "";
        unsigned int right = strs[0].size();
        
        for (unsigned int i = 1; i < strs.size(); ++i) {
            for (unsigned int j = 0; j < right; ++j) {
                if (strs[0][j] != strs[i][j]) {
                    right = j;
                    break;
                }
            }
        }
        return strs[0].substr(0,right);
    }
};
上一篇下一篇

猜你喜欢

热点阅读