代码拉取完成,页面将自动刷新
/*
* @lc app=leetcode.cn id=14 lang=cpp
*
* [14] 最长公共前缀
*/
#include <bits/stdc++.h>
using namespace std;
// @lc code=start
class Solution
{
public:
struct
{
char ch;
int index;
bool operator()(const string &s) const
{
if (index >= s.length())
return false;
return s[index] == ch;
}
} is_common_char;
string longestCommonPrefix(vector<string> &strs)
{
if (strs.size() == 1)
return strs[0];
int i = 0;
do
{
is_common_char.ch = strs[0][i];
is_common_char.index = i;
if (!all_of(strs.begin() + 1, strs.end(), is_common_char))
break;
} while (++i);
string result = i == -1 ? "" : strs[0].substr(0, i);
return result;
}
};
// @lc code=end
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。