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 ...
随机推荐
- Mysql性能优化之缓存参数优化
数据库属于 IO 密集型的应用程序,其主要职责就是数据的管理及存储工作.而我们知道,从内存中读取一个数据库的时间是微秒级别,而从一块普通硬盘上读取一个IO是在毫秒级别,二者相差3个数量级.所以,要优化 ...
- QT:QBitArray
QbitArray类提供位操作序列. include<QBitArray> 公有函数: QBitArray () QBitArray ( int size , bool valu ...
- 神奇的 BlocksKit(1):源码分析(上)
高能预警:本篇文章非常长,因为 BlocksKit 的实现还是比较复杂和有意的.这篇文章不是为了剖析 iOS 开发中的 block 的实现以及它是如何组成甚至使用的,如果你想通过这篇文章来了解 blo ...
- asp.net分页控件库
AspNetPager分页控件 AspNetPager分页控件解决了分页中的很多问题,直接采用该控件进行分页处理,会将繁琐的分页工作变得简单化,下面是我如何使用AspNetPager控件进行分页处理的 ...
- android之错误汇总
A.错误:生成android源码索引期间使用mmm命令报错 B.解决: 或者 . build/envsetup.sh 依据自己的环境脚本而定 未完待续.....
- spring MVC fromeWork 與webwork2 mvc 比較
http://www.blogjava.net/xxxzheng/articles/7614.html 在当今的MVC framework里,似乎Webwork2逐渐成为主流, Webwork2+Sp ...
- [ lucene高级 ] 研讨如何进行Lucene的分布式应用
http://www.cnblogs.com/huangfox/archive/2010/10/15/1852206.html Lucene是个高度优化的倒转索引搜索引擎.它将倒转的索引存储在定制的文 ...
- view,SurfaceView,GLSurfaceView的关系和区别
如果你的游戏不吃CPU,用View就比较好,符合标准Android操作方式,由系统决定刷新surface的时机. 但如果很不幸的,你做不到不让你的程序吃CPU,你就只好使用SurfaceView来强制 ...
- 20160328 javaweb Cookie 小练习
利用cookie实现历史记录浏览: 由于是简单演示,所以直接用javabean 取代数据库了 数据存储类: package com.dzq.dao; import java.util.*; impor ...
- Android获取屏幕尺寸大小
官方API: A structure describing general information about a display, such as its size, density, and fo ...