[Leetcode]Longest Common Prefix
来源:互联网 发布:大数据时代联系的特点 编辑:程序博客网 时间:2024/06/16 14:49
Write a function to find the longest common prefix string amongst an array of strings.
class Solution {public: /*algorithm loop from len = 0 to maxlen of strings each loop check whehter the substr(0,len) is all prefix for all strings time O(n*n) space O(1) */ bool isMatch(string &s,int len,string &match){ int i = 0; while(i < len && s[i] == match[i])++i; return i == len; } string longestCommonPrefix(vector<string>& strs) { if(strs.size() < 1)return ""; int len = 0; int maxLen = 1; bool exit = false; for(;len <= maxLen;len++){ for(int i = 0;i < strs.size();i++){ maxLen = max(maxLen,(int)strs[i].size()); if(!isMatch(strs[i],len,strs[0])){ exit = true; break; } } if(exit)break; } return len > 0 ?strs[0].substr(0,len - 1):""; }};
0 0
- LeetCode: Longest Common Prefix
- LeetCode Longest Common Prefix
- LeetCode : Longest Common Prefix
- [Leetcode] Longest Common Prefix
- [LeetCode] Longest Common Prefix
- [LeetCode]Longest Common Prefix
- LeetCode-Longest Common Prefix
- [LeetCode] Longest Common Prefix
- LeetCode - Longest Common Prefix
- leetcode Longest Common Prefix
- LeetCode | Longest Common Prefix
- Leetcode: Longest Common Prefix
- LeetCode: Longest Common Prefix
- 【LeetCode】Longest Common Prefix
- [LeetCode] Longest Common Prefix
- [LeetCode]Longest Common Prefix
- leetcode: Longest Common Prefix
- LeetCode Longest Common Prefix
- C/C++ 程序设计员应聘常见面试试题深入剖析
- 三个学生,每人修四科,计算所有学生成绩的平均分,并输出某个学生的成绩
- linux下用cmake
- poj 2886(约瑟夫+单点修改+反素数)
- malloc/free与new/delete的区别
- [Leetcode]Longest Common Prefix
- Linux学习笔记----目录处理命令---mv ---剪切和改名
- 托福写作1
- 生成csv文件 可以使用excell读取
- 说一下视频播放跟随屏幕旋转,以及activity涉及到的周期问题
- HDU 1238 Substrings
- 希尔排序(C++版)
- 并发编程
- WCF问题集锦:基础连接已经关闭 接收时发生错误