1 Star 0 Fork 0

Janu C./力扣刷题记录

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
14.最长公共前缀.cpp 874 Bytes
一键复制 编辑 原始数据 按行查看 历史
Janu C 提交于 2022-10-16 01:07 +08:00 . 同步之前的代码
/*
* @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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/janu-c/LeetCode.git
[email protected]:janu-c/LeetCode.git
janu-c
LeetCode
力扣刷题记录
master

搜索帮助