Longest common prefix | leetcode
Write a function to find the longest common prefix string amongst an array of strings.
思路:要去是寻找字符串vector里最长的公有前缀。
1。构造迭代器,遍历vector搜索最小string长度。然后从第一个字符开始进行遍历,如果遇到不一样的字符即返回,全部遍历完毕没问题就把它添加到前缀字符串里。
这样做效率比较差,有待改进。
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
unsigned minLen = 0xffffffff;
unsigned i = ;
string res = "";
vector<string>::iterator it;
for (it = strs.begin();it != strs.end(); it++) {
cout << (*it).size();
minLen = ((*it).size() < minLen) ? ((*it).size()) : minLen;
}
// cout << "minlen is " << minLen << endl;
if ((!minLen) || (minLen == 0xffffffff))
return "";
// cout << "minlen is " << minLen << endl;
while (i != minLen) {
char temp;
bool flag = false;
it = strs.begin();
temp = (*it)[i];
for (it = strs.begin(); it != strs.end(); it++) {
if (temp != (*it)[i])
return res;
}
res += temp;
i ++;
}
return res;
}
};
Longest common prefix | leetcode的更多相关文章
- Longest Common Prefix [LeetCode 14]
1- 问题描述 Write a function to find the longest common prefix string amongst an array of strings. 2- 思路 ...
- Longest Common Prefix leetcode java
题目: Write a function to find the longest common prefix string amongst an array of strings. 题解: 解题思路是 ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【一天一道LeetCode】#14 Longest Common Prefix
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- LeetCode题解(14)--Longest Common Prefix
https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
随机推荐
- [Erlang]怎样加入自己的BIF
步骤 1. 执行configure 2. 将你的bifs加入至erts/emulator/beam/bif.tab bif re:grep/2 bif re:compile/1 3. 创建一个C代码文 ...
- Windows 自己主动关机命令 shuntdown
以下介绍一个在Windows XP下实现定时关机的简单方法. 一 .倒计时关机: 指定系统在10分钟后自己主动关闭:点击“開始→执行”,输入命令“Shutdown -s -t 60”(注意: ...
- Android记录6--ViewPage+Fragment的使用例子
Android记录6--ViewPage+Fragment的使用例子 2013年9月6日Fragment学习 Fragment这个东西,我到现在才接触到,之前没有用到过,关于Fragment这个东西在 ...
- 基于u盘安装centos6.0
本人在dell笔记本上尝试安装centos6.0,与win7形成双系统,安装过程如下: 1.使用ultraliso制作u盘启动盘,启动盘以centos6.0的映像文件为源头制作: 2.制作完成后,删除 ...
- CentOS 6.6 yum源完全配置
原文地址 http://blog.csdn.net/halazi100/article/details/41311837 一 yum 简介 yum,是"Yellow dog Updater, ...
- how to forget about delta cycles for RTL design
A delta cycle is a VHDL construct used to makeVHDL, a concurrent language, executable on asequential ...
- Ubuntu GNOME 安装日语输入法(收集)
原网址是:code.duffy.jp/add-japanese-input-to-gnome/ 1)在终端(Ctrl + Alt + t)输入:sudo apt-get install ibus-an ...
- 第十篇:web之前端之django一些feature
前端之django一些feature 前端之django一些feature 本节内容 cookie session 跨站请求保护 分页 序列化 model模块 CBV和FBV 模板渲染对象 1. ...
- 如何理解systemstate
什么是systemstate一个systemstate是由在实例中调用生成systemstats时由每一个进程的进程状态组成.而每一个进程状态是由每一个进程所持有的当前对象所对应的详细对象状态信息组成 ...
- (二)Hibernate4 CRUD 体验
所有的学习我们必须先搭建好hibernate的环境(1.导入对应的jar包,2.hibernate.cfg.xml,3.XXXX.hbm.xml) 第一节:HibernateUtil 封装 导入对应的 ...