最长公共前缀 -LintCode
来源:互联网 发布:unity3d 角色动作 编辑:程序博客网 时间:2024/05/29 11:13
给k个字符串,求出他们的最长公共前缀(LCP)
样例
在 “ABCD” “ABEF” 和 “ACEF” 中, LCP 为 “A”
在 “ABCDEFG”, “ABCEFG”, “ABCEFA” 中, LCP 为 “ABC”
#ifndef C78_H#define C78_H#include<iostream>#include<vector>#include<string>using namespace std;class Solution {public: string longestCommonPrefix(vector<string> &strs) { // write your code here int k = strs.size(); int minLength = INT_MAX; int pos=0; for (auto ss : strs) { if (ss.size() <= minLength) minLength = ss.size(); } for (int j = 0; j < k - 1; ++j) { for (int i = 0; i < minLength; ++i) { if (strs[j][i] != strs[j + 1][i]) { pos = i; //相同的元素个数 break; } if (i == minLength-1) { pos = minLength;//前缀是它本身 break; } } } string s(strs[0],0,pos); return s; }};#endif
阅读全文
0 0
- LintCode-最长公共前缀
- LintCode 最长公共前缀
- lintcode ---- 最长公共前缀
- LintCode : 最长公共前缀
- lintcode,最长公共前缀
- 最长公共前缀 -LintCode
- [Lintcode]最长公共前缀
- lintcode -- 最长公共前缀
- lintcode 最长公共前缀
- Lintcode(6)-最长公共前缀
- lintcode-最长公共前缀-78
- lintcode之最长公共前缀
- LintCode(M)最长公共前缀
- LintCode 78.最长公共前缀
- LintCode 78 最长公共前缀
- [Lintcode] #78 最长公共前缀
- [LintCode 78] 最长公共前缀(Python)
- lintcode python ——最长公共前缀(LCP)
- Qt自定义加载控件Spinner
- 写的程序出现"open clipboard失败"的解决办法
- Python SqlAlchemy数据库持久化使用方法
- java 事件与委托
- Eclipse注释模板设置详解
- 最长公共前缀 -LintCode
- Python标准模块--ContextManager(上下文管理器)
- markDown的使用技巧--空格缩进、颜色
- redis 报错 Redis protected-mode 配置文件没有真正启动
- 设计模式-观察者模式
- Android Butterknife 8.4.0 使用方法总结
- Android实现多进程方式及注意
- jacob word 转pdf
- 关于librtmp接收数据(接收网络电视的数据流)