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 ...
随机推荐
- 操作系统-mac安装linux(Ubuntu)
mac安装linux 安装linux,分为两步: 1. 制作linux启动U盘 2. 通过启动U盘,安装Ubuntu系统 Mac OS X下创建USB启动盘 格式化USB驱动盘 下载Ubuntu桌面版 ...
- p
都不知道简历去投什么地方.游戏都卖不出去,又做不出口碑好的.这些人是心存侥幸还是心存坚持. 感觉自己搞不清楚就很难再出发.
- iOS之CAKeyframeAnimation关键帧动画详解
CABasicAnimation算是CAKeyFrameAnimation的 特殊情况,即不考虑中间变换过程,只考虑起始点与目标点就可以了.而CAKeyFrameAnimation则更复杂一些,允许我 ...
- vi快捷键必知必会
文本编辑器是所有计算机系统中最常用的一种工具.UNIX下的编辑器有ex,sed和vi等,其中,使用最为广泛的是vi,而vi命令繁多,论坛里好像这方面的总结不多,以下稍做总结,以资共享!渴望更正和补充! ...
- Form开发中组件控制的几个常用方法
转自:http://oracleseeker.com/2009/09/01/graphical_component_control_in_oracle_ebs_form/ 在Oracle EBS 的F ...
- mysql 中间件 分析
360的Atlas 1.读写分离 2.从库负载均衡 3.IP过滤 4.自动分表 5.DBA可平滑上下线DB 6.自动摘除宕机的DB altas 在10000/s的请求量级应该是毫无问题的 https: ...
- Redis与Memcached对比
Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富,有字符串.链表.集合和有序集合.支持在服务器端计算集合的并,交和补集等.还支持多 ...
- C#相关时间DateTime格式化
C#代码中时间转换为2016-01-24 12:12:12需要如下操作: DateTime.Parse(sj).ToString("yyyy-MM-dd HH:m:ss") 但是O ...
- MVC小系列(十八)【给checkbox和radiobutton添加集合的重载】
mvc对DropDownListFor的重载很多,但对checkbox和radiobutton没有对集合的重载 所以该讲主要针对集合的扩展: #region 复选框扩展 /// <summary ...
- java 利用注解实现BaseDao 增删查改
第一步,编写两个注解类,用于表明实体类对应的表名及字段. TableInfo.java 此注解用于标注表名及主键名 import static java.lang.annotation.Element ...