[LeetCode] Longest Common Prefix 字符串公有前序
Write a function to find the longest common prefix string amongst an array of strings.
- 判断输入的字符串数量,小于2时候做出相应返回。
- 获取最短字符串的长度。
- 设定标记flag,控制跳出循环,与长度返回的长度len。
- 在最短字符串长度的范围内循环。
- 循环中每次遍历全部字符串len 位的字符。
- 遇到不同设置flag 跳出循环,如果全部都相同len+1 进入下次循环。
- 返回长度。
其实可以简单点不求最短字符串长度,将这一步放入到判断是否相同时候。
#include <iostream>
#include <vector>
#include <string>
using namespace std; class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
int nvec = strs.size();
if(nvec<) return "";
if(nvec<) return strs[];
int nmin = strs[].length(),len=;
bool flag = true;
for(int i =;i<nvec;i++){
if(nmin>strs[i].length()) nmin = strs[i].length();
}
while(len<nmin){
for(int i=;i<nvec&&flag;i++){
if(strs[i][len]==strs[][len]) continue;
flag = false;
}
if(!flag) break;
len++;
}
return strs[].substr(,len);
}
}; int main()
{
vector<string> strs={"","","","",""};
Solution sol;
cout<<sol.longestCommonPrefix(strs)<<endl;
return ;
}
[LeetCode] Longest Common Prefix 字符串公有前序的更多相关文章
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- LeetCode: Longest Common Prefix 解题报告
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- LeetCode——Longest Common Prefix
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. class Solutio ...
- Leetcode::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
- [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀
题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...
- leetcode Longest Common Prefix 多个字符串的最长字串
public class Solution { public String get(String a,String b) { if(a==""||b=="") ...
- LeetCode Longest Common Prefix 最长公共前缀
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
- leetcode Longest Common Prefix python
class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str ...
随机推荐
- python笔记-dict字典的方法2
#!/usr/bin/env python #-*- coding:utf-8 -*- ''' 概述: 使用键值(key-value)存储,具有极快的查找速度 注意:字典是无序的 key的特性: 1. ...
- tp5查询
TP5的EXP.批量查询.聚合查询等. <!--more--> //使用EXP条件表达式,表示后面是原生的SQL表达式 $result = Db::table('think_inno')- ...
- 学习python第二天 流程判断
while循环age_of_Jim = 56 count = 0 #开始计数while True: #循环代码 if count ==3:#如果次数=3 break#退出 guess_age = in ...
- BFS:胜利大逃亡
解题心得: 1.水题,主要主意好一个点就好. 2.注意x.y.z坐标的选取就好. 题目: Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城 ...
- [Poj3133]Manhattan Wiring (插头DP)
Description 题目大意:给你个N x M(1≤N, M≤9)的矩阵,0表示空地,1表示墙壁,2和3表示两对关键点.现在要求在两对关键点之间建立两条路径,其中两条路径不可相交或者自交(就是重复 ...
- 51nod_1199 树的先跟遍历+区间更新树状数组
题目是中文,所以不讲题意 做法顺序如下: 使用先跟遍历,把整棵树平铺到一维平面中 使用自己整的区间更新树状数组模板进行相关操作. http://www.cnblogs.com/rikka/p/7359 ...
- opencv使用日记之一:平台搭建Mat类以及图像的读取修改
平台搭建就摸了一整天时间,真的是...不说了,最后我选择的是 opencv3.0(2015/06/04) + win7 + vs2012 注意opencv的版本不同导入的库文件是不一样的,所以请 ...
- [原]sencha touch之布局
今天记录一下关于sencha touch中的几种布局,其实很简单的,还是直接上代码,一目了然 1:box布局,其实就是vbox和hbox,说白了一个是横着摆放,一个是竖着摆放 Ext.applicat ...
- Redis实现之RDB持久化(一)
RDB持久化 Redis是一个键值对数据库服务器,服务器中通常包含着任意个非空数据库,而每个非空数据库中又可以包含任意个键值对,为了方便起见,我们将服务器中的非空数据库以及它们的键值对统称为数据库状态 ...
- complex类的定义、实现
复数类complex的定义.实现(求模.复数加法) #include <iostream> #include <cmath> using namespace std; clas ...