31-Longest Common Prefix
- Longest Common Prefix My Submissions
Difficulty: Easy
Write a function to find the longest common prefix string amongst an array of strings.
难度:easy
思路:求解所有字符串的最长前缀
首先拿第一个字符串作为标准,从第一个字符串开始检测,如果其他的字符串的相应位置没有该字符,则输出,全有则继续
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
vector<char> vps;
int n=strs.size();
if(n<1)return "";
for(int j=0;j<strs[0].size();++j){
int flag =0; //标记在这个位置是否所有串都有当前字符
for(int i=1;i<n;++i){
if(strs[i].size()<=j||(j<strs[i].size()&&(strs[0][j]!=strs[i][j]))){
flag =1;
break;
}
}
if(flag==1) break;
vps.push_back(strs[0][j]);
}
if(vps.size()==0)return "";
return string(vps.data(),0,vps.size());
}
};
31-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
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- LintCode 78:Longest Common Prefix
public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...
- [LintCode] Longest Common Prefix 最长共同前缀
Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...
- 14. Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...
- Leetcode Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- [LeetCode] 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. public class ...
- No.014:Longest Common Prefix
问题: Write a function to find the longest common prefix string amongst an array of strings. 官方难度: Eas ...
- 68. Longest Common Prefix
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- No.014 Longest Common Prefix
14. Longest Common Prefix Total Accepted: 112204 Total Submissions: 385070 Difficulty: Easy Write a ...
随机推荐
- dwr简单应用及一个反向ajax消息推送
由于项目中最近需要用到dwr实现一些功能,因此在网上和dwr官网上找了一些资料进行学习.在此记录一下.(此处实现简单的dwr应用和dwr消息反向推送) 一.引入dwr的包 <dependency ...
- activiti会签 多实例例子
在实际的业务中,可能存在存在这么一种情况,当流程运行到某一个环节时,可能需要同时多个人的参与,才可以完成此环节.此时就可以用到activiti的多实例来解决此问题. 一.将一个节点设置成多实例的方法: ...
- Hadoop的安装与部署
一.硬件及环境 服务器:3台,IP分别为:192.168.100.105.192.168.100.110.192.168.100.115 操作系统:Ubuntu Server 18.04 JDK:1. ...
- 『学了就忘』Linux基础 — 15、了解Linux系统的目录结构
目录 1.一级目录说明 (1)一级目录列表 (2)/bin/和/sbin/目录说明 (3)/boot/目录说明 (4)/lib/和/lib64/目录说明 (5)/lost+found/目录说明 (6) ...
- 关于linux的fork的一点学习总结
最近操作系统的实验要用到fork,于是去搜索了一下资料,很幸运地在博客中找到一篇深度好文: http://blog.csdn.net/jason314/article/details/5640969 ...
- Vue-cli4.xPC端项目Rem适配
适配准备 安装 (amfe-flexible) 和(postcss-px2rem) 1, 安装依赖并在main.js中引入该依赖 npm i amfe-flexible import "am ...
- linux 安装rabbitmq
1.安装rabbitmq会依赖erlang.socat.unixodbc 下载 unixODBC-2.3.7.tar.gz ,创建路径/usr/local/unixODBC-2.3.7,解压到该路径下 ...
- vue-router 4 你真的熟练吗?
虽然 vue-router 4 大多数 API 保持不变,但是在 vue3 中以插件形式存在,所以在使用时有一定的变化.接下来就学习学习它是如何使用的. 一.安装并创建实例 安装最新版本的 vue-r ...
- 【java+selenium3】自动化cookie操作+图形验证码处理 (十五)
一.cookie操作 1.获取浏览器所有的cookie import java.util.Set; import org.openqa.selenium.Cookie; //获取浏览器所有的cooki ...
- C 函数指针语法总结
C 函数指针语法总结 函数指针 定义 每一个函数都占用一段内存单元,它们有一个起始地址,指向函数入口地址的指针称为函数指针. 注意:函数指针的本质是一个指针变量,且指针指向的函数的入口地址. 语法 返 ...