【Longest Common Prefix】cpp
题目:
Write a function to find the longest common prefix string amongst an array of strings.
代码:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if ( strs.size()== ) return "";
std::string tmp_str = strs[];
for ( size_t i = ; i < strs.size(); ++i )
{
size_t j = ;
for ( ; j < std::min(tmp_str.size(), strs[i].size()); ++j )
if ( tmp_str[j]!=strs[i][j] ) break;
tmp_str = tmp_str.substr(,j);
}
return tmp_str;
}
};
tips:
空间复杂度O(n1),时间复杂度O(n1+n2...)
strs中的每个string串依次两两比较,保留下这一次比较得出的公共前缀为tmp_str;下次再比较时,可以用上次得到的公共前缀作为目标字符串。
这个解法的好处就是代码比较consice,但空间复杂度和时间复杂度其实都不是最优的。
=====================================================
第二次过这道题,自己写出来了代码,调了两次AC了。
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
if ( strs.size()== ) return "";
if ( strs.size()== ) return strs[];
string common_pre = strs[];
for ( int i=; i<strs.size(); ++i )
{
if ( common_pre.size()== ) break;
int common_len = min( common_pre.size(), strs[i].size());
for ( int j= ;j<min( common_pre.size(), strs[i].size()); ++j )
{
if ( common_pre[j]!=strs[i][j] )
{
common_len = j;
break;
}
}
common_pre = common_pre.substr(,common_len);
}
return common_pre;
}
};
【Longest Common Prefix】cpp的更多相关文章
- LeetCodeOJ刷题之14【Longest Common Prefix】
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- 【Longest Palindromic Substring】cpp
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- 【Longest Valid Parentheses】cpp
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- 【Longest Consecutive Sequence】cpp
题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...
- 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum
[Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- 【JAVA、C++】LeetCode 014 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...
- 【LeetCode】14. Longest Common Prefix 最长前缀子串
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...
- 【LeetCode】14 - Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...
随机推荐
- Apache与Nginx的优缺点
http://weilei0528.blog.163.com/blog/static/206807046201321810834431 Apache与Nginx的优缺点比较 1.nginx相对于apa ...
- xode View 的封装
1.Xcode自带头文件的路径 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Develo ...
- Dockpanel的控件加载问题
1. 正确加载模式:panel.ControlContainer.Controls.Add(control); 如果用panel.Controls.Add(control);则可能出现模块发生位移问题 ...
- 在xml中添加array
在values建立arrays(名字可自定义)的xml: <?xml version="1.0" encoding="utf-8"?> <re ...
- 靶形数独 (codevs 1174)题解
[问题描述] 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向Z 博士请教,Z 博士拿出了他最近发明的“ ...
- Dapper多表查询(列名重复,类字段重复)映射方案
1. 一个主名,一个别名,设计时候属性和字段命名不同. 这样主名和别名都可以用的,在主名与别人重复时候用别名(别名可以设计的明确一点长一点,比如类名和字段结合) 2. 或者找一个字段多的直接继承出一个 ...
- 在EF的code frist下写稳健的权限管理系统:开篇(一)
环境:EF6.0.0.0+Autofac3.5.0.0+MVC4.0+pure6.0+Jquery IDE:vs2012,数据库:vs2008r2 搭建环境如下: 我给它取名字为cactus:仙人球, ...
- 数组(Array),二维数组,三维数组
数组(Array):相同类型数据的集合就叫做数组. (一)定义数组的方法: A) type[] 变量名 = new type[数组中元素的个数] 例如: int[] a = new int[10] ; ...
- C/C++ 内存管理 (《高质量C++》-- 整理笔记)
内存管理是我们在编程时经常遇到的问题,而关于内存管理的问题往往会导致我们无从下手,这篇随笔是我阅读<高质量C++>第7章“内存管理”时一些总结. 1.内存分配方式 在C++中内存分为5个区 ...
- C++经典书籍推荐
<C++程序设计语言> <C++语言设计与演化> <C++标准程序库> <EFFECTIVE C++ 中文版> <MORE EFFECTIVE C ...