【leetcode】Longest Common Prefix
来源:互联网 发布:天津网络危机公关公司 编辑:程序博客网 时间:2024/06/05 02:04
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
思路:
1)找到最短的字符串,因为最长子串不会比这个长;
2)一个一个字符比较,直到有不同的,退出,输出res
class Solution {public: string longestCommonPrefix(vector<string>& strs) { string res; int n=strs.size(); if(n==0) return res; if(n==1) return strs[0]; string small; int length=100000; for(int i=0;i<n;i++) { if(length>strs[i].length()) {length=strs[i].length();small=strs[i];} } for(int i=0;i<length;i++) { for(int j=0;j<n;j++) { if(strs[j][i]!=small[i]) return res; } res.push_back(small[i]); } return res; }};
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
- 2015百度之星资格赛1004放盘子
- python类定义的讲解
- tesseract3.0.2在mac 10.10上的编译 xcode6
- Cocos2d-x 屏幕适配
- 病毒分析师初探-转载
- 【leetcode】Longest Common Prefix
- Nvidia Maxwell Architecture
- openOCD的使用1: 应用openOCD和Jlink仿真器连接Freescale K60系列 MCU
- 【leetcode】【单链表】【143】Reorder List
- android渠道包自动化验证
- hdu 2030 汉字统计 (java)
- 机房收费系统 Section One
- 牛腩新闻发布系统-零碎拾起
- Java第二次作业