Longest Common Prefix
2015-09-01 10:36
225 查看
Description:
Write a function to find the longest common prefix string amongst an array of strings.(最长公共字串)
Code:
Write a function to find the longest common prefix string amongst an array of strings.(最长公共字串)
Code:
string merge(string&str1, string&str2) { string result=""; int n = (str1.size()<=str2.size())?str1.size():str2.size(); for (int i = 0; i < n; ++i) { if (str1[i]==str2[i]) result=result+str1[i]; else return result; } return result; } string longestCommonPrefix(vector<string>& strs, int start, int end) { if (start < end) { int middle = (start+end)/2; string str1 = longestCommonPrefix(strs, start, middle); if (str1=="") return str1; string str2 = longestCommonPrefix(strs, middle+1, end); if (str2=="") return str2; return merge(str1, str2); } else return strs[start]; } string longestCommonPrefix(vector<string>& strs) { if (strs.size()==0) return ""; return longestCommonPrefix(strs,0,strs.size()-1); }
相关文章推荐
- Continuous Subarray Sum II
- mysql常用show命令
- 说说最近一段时间找工作的事(Java后端开发)
- 2016华为机试-自动售货机
- Xilinx Spartan 6 驱动ADS1278/4
- iOS thread1:exc_bad)access(code=exc_1386_gpflt) 调试方案
- codeforces 10C C. Digital Root(数论)
- 命名规范(2)通用命名约定
- 【前端学习笔记】2015-09-01 附 split()方法、readyState
- 能耗折算标准煤公式
- js原生无缝滚动demo
- leetcode Linked List Cycle II
- poj 2286 The Rotation Game IDA*算法
- php第一阶段html_table(2)
- 自动释放池什么时候创建,什么时候销毁?
- js,php 使用正则表达式解析GPS数据
- 项目:××官网改版总结经验和收获
- mysql常用语句
- 8月国内网站流量统计TOP5:360安全中心季军
- android调整版本问题记载