Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
52 lines (51 sloc) 1.14 KB
/*
* @lc app=leetcode.cn id=14 lang=cpp
*
* [14] 最长公共前缀
*
* https://leetcode-cn.com/problems/longest-common-prefix/description/
*
* algorithms
* Easy (31.94%)
* Total Accepted: 64K
* Total Submissions: 196.9K
* Testcase Example: '["flower","flow","flight"]'
*
* 编写一个函数来查找字符串数组中的最长公共前缀。
*
* 如果不存在公共前缀,返回空字符串 ""。
*
* 示例 1:
*
* 输入: ["flower","flow","flight"]
* 输出: "fl"
*
*
* 示例 2:
*
* 输入: ["dog","racecar","car"]
* 输出: ""
* 解释: 输入不存在公共前缀。
*
*
* 说明:
*
* 所有输入只包含小写字母 a-z 。
*
*/
// 2Darray。纵向遍历。优化版,substr
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if(strs.empty()) return "";
for(int col=0;col<strs[0].size();col++)
{
for(int row=0;row<strs.size();row++)
{
if(strs[row].size()<=col||strs[row][col]!=strs[0][col])
return strs[0].substr(0,col);
}
}
return strs[0];
}
};
You can’t perform that action at this time.