LeetCode 14 Longest Common Prefix最长公共前缀LCA 暴力 C++实现

传送门:题目

暴力就好,朴素字符串大法好

AC代码:

#include 
using namespace std;
#define INF 0x3f3f3f3f
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        string ans = "";
        if (strs.empty())
            return ans;
        int mmin = INF,i,j;
        for (int i = 0; i < strs.size(); i++)
            if (mmin > strs[i].size())
                mmin = strs[i].size();
        for (j = 0; j < mmin; j++) {
            for (i = 0; i < strs.size() - 1; i++) {
                if (strs[i][j] != strs[i + 1][j]) {
                    return ans;
                }
            }
            ans += strs[i][j];
        }
        return ans;
    }
};

你可能感兴趣的:(字符串)