[LeetCode]14. Longest Common Prefix
来源:互联网 发布:上海华讯网络上市了吗 编辑:程序博客网 时间:2024/06/11 17:41
题目:Write a function to find the longest common prefix string amongst an array of strings.
寻找一组字符串中的最大公共字符串
#include<iostream>#include<string>#include<vector>using namespace std;class Solution {public:string longestCommonPrefix(vector<string> &strs) {int n = strs.size();string res;if (n == 0)return res;for (int pos = 0; pos < strs[0].size(); pos++)//最长前缀的长度不超过strs[0].size(),逐个字符的比较{for (int k = 1; k < n; k++)//strs[0]的第pos个字符分别和strs[1...n-1]的第pos个字符比较{if (strs[k].size() == pos || strs[k][pos] != strs[0][pos])return res;}res.push_back(strs[0][pos]);}return res;}};
0 0
- [leetcode] 14. Longest Common Prefix[leetcode] 14. Longest Common Prefix
- [LeetCode]14.Longest Common Prefix
- LeetCode 14. Longest Common Prefix
- LeetCode --- 14. Longest Common Prefix
- LeetCode 14.Longest Common Prefix
- [Leetcode] 14. Longest Common Prefix
- 【leetcode】14. longest common prefix
- [leetcode] 14.Longest Common Prefix
- [leetcode] 14. Longest Common Prefix
- LeetCode - 14. Longest Common Prefix
- Leetcode-14. Longest Common Prefix
- Leetcode 14. Longest Common Prefix
- 14. Longest Common Prefix LeetCode
- leetcode 14. Longest Common Prefix
- leetcode 14. Longest Common Prefix
- leetcode 14. Longest Common Prefix
- LeetCode--14. Longest Common Prefix
- Leetcode 14. Longest Common Prefix
- 服务器分布式环境搭建【一】
- 关于webview加载网页出现打不开网页或者空白页面情况的处理
- Problem A: STL——邮票
- ElasticsearchCRUD使用(十)【Elasticsearch类型与ElasticsearchCRUD的映射】
- Myeclipse连接Mysql数据库时报错:Error while performing database login with the pro driver:unable
- [LeetCode]14. Longest Common Prefix
- swing与awt以及Adapte
- 发现vjudge用搜狗浏览器无法登陆
- Josephus环问题求解(Java实现)
- js闭包中this的指向问题及三种解决方法
- Problem B: STL——多重集的插入和删除
- LeetCode:492. Construct the Rectangle
- 首次安装Robot Framework中遇到的坑
- Problem C: 分数类的输出