lintcode: Longest Common Prefix
来源:互联网 发布:淘宝查小号信誉 编辑:程序博客网 时间:2024/06/08 10:12
Longest Common Prefix
Given k strings, find the longest common prefix (LCP).
Example
For strings "ABCD"
, "ABEF"
and "ACEF"
, the LCP is "A"
For strings "ABCDEFG"
, "ABCEFG"
and "ABCEFA"
, the LCP is "ABC"
Tags
class Solution {public: /** * @param strs: A list of strings * @return: The longest common prefix */ string longestCommonPrefix(vector<string> &strs) { // write your code here if (strs.size() == 0) return ""; // 这个条件判断非常重要 int compLen = INT_MAX; for (int i=0; i<strs.size(); i++) compLen = min((int)strs[i].size(), compLen); string retStr = ""; for (int j=0; j<compLen; j++) { char pivot = strs[0][j]; for (int i=1; i<strs.size(); i++) { if (strs[i][j] != pivot) { if (j > 0) { return strs[i].substr(0, j); } else { return ""; } } } } return strs[0].substr(0, compLen); }};
0 0
- lintcode: Longest Common Prefix
- [LintCode]Longest Common Prefix(Python)
- LeetCode: Longest Common Prefix
- LeetCode Longest Common Prefix
- LeetCode : Longest Common Prefix
- [Leetcode] Longest Common Prefix
- leetcode6:Longest Common Prefix
- leetcode22: longest common prefix
- [LeetCode] Longest Common Prefix
- Leetcode14: Longest Common Prefix
- Longest Common Prefix
- Longest Common Prefix
- Longest Common Prefix
- [LeetCode]Longest Common Prefix
- Longest Common Prefix
- LeetCode-Longest Common Prefix
- Longest Common Prefix
- [LeetCode] Longest Common Prefix
- 23 运算符重载(二)
- Linux CPU频率控制
- OVS命令详解
- 8.ARM工作模式和寄存器及异常
- Hive数据导入方案—使用ORC格式存储hive数据
- lintcode: Longest Common Prefix
- eclipse和myeclipse编辑器中代码 “ import="java.sql.* " 报错的解决方案
- thread 退出状态如何获取
- windows(win7,win8,xp)hosts文件找不到原因分析及解决方法
- Android ProgressBar 样式大全
- Java基础------知识点整理(四)-----final关键字
- 迷宫
- hdu1242
- Disruptor:为什么会这么快?(三)揭秘内存屏障