Leetcode题解
来源:互联网 发布:ipad视频怎么导入mac 编辑:程序博客网 时间:2024/06/04 18:44
Write a function to find the longest common prefix string amongst an array of strings.
链接
给定的字符串存在一个数组中,将第一个字符串作为参考,把后续的字符串依次与strs[0]进行比较即可,双重循环即可完成。
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题解
- leetcode题解
- LeetCode 题解
- leetCode题解
- leetcode题解
- leetcode题解
- leetcode题解
- LeetCode题解
- LeetCode题解
- Leetcode题解
- Leetcode题解
- Leetcode题解
- Leetcode题解
- Leetcode题解
- Leetcode题解
- Leetcode题解
- Leetcode题解
- Leetcode题解
- nodejs发送邮件
- 为什么CSDN中个人主页和我的博客两大版块分开这么明显
- Arduino学习笔记(四)Matlab串口控制Arduino转动电机
- java.io.IOException: No FileSystem for scheme: hdfs异常解决
- https认证详解
- Leetcode题解
- 任务,行动,目标、绩效管理,计划
- 计算机网络5层架构
- Reactive Programming with RxJava,介绍一本书和Rx
- Webpack 入门指南
- org.apache.hadoop.io.compress系列1-认识解码器/编码器
- 仿ASSISTIVETOUCH效果的浮动层小球
- maven笔记
- WPF AdornerLayer.GetAdornerLayer返回null