LeetCode Longest Common Prefix 最长公共前缀

题意:给多个字符串,返回这些字符串的最长公共前缀。
思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较。否则终止比较,返回前缀。可能有一个字符串会比较短,所以前缀最长也只是最短字符串的长度。
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
string ans="";
if(strs.empty()) return ans;
int has[], j=;
while()
{
memset(has,,sizeof(has));
for(int i=; i<strs.size(); i++)
{
if(j==strs[i].size()) return ans;
has[strs[i][j]]++;
}
if(has[strs[][j]]!=strs.size()) return ans;
ans+=strs[][j++];
}
}
};
AC代码
LeetCode Longest Common Prefix 最长公共前缀的更多相关文章
- # Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- [LeetCode]14. Longest Common Prefix最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)
1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...
- 【LeetCode】Longest Common Prefix(最长公共前缀)
这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...
- [leetcode]14. Longest Common Prefix 最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- 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. If there is n ...
随机推荐
- EF框架批量更新
var customers = db.Customers.Where(c => c.name=='小明'); foreach (var customer in customers) { cust ...
- php用fsockopen实现post提交数据并获得返回数据
/** * 函数介绍: 用于post方式提交数据 * 输入参数: 完整url, 数据 * 返回值 : 接口返回值 */ function post_it($url, $data = '', $time ...
- Linux 删除文件夹和文件的命令
linux删除目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可.直接rm就可以了,不过要加两个参数-rf 即:rm -rf 目录名字-r 就 ...
- lintcode:验证二叉查找树
题目 给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值. 节点的右子树中的值要严格大于该节点的值. 左右子树也必须是二叉查找树. 一个 ...
- c# 事件为何要继承EventArgs
1:继承EventArgs是表示该类可作为事件,删掉了就默认继承object,没人会说你错 ----就是说事件不继承EventArgs 也没有错,也能正常运用,那么继承他的意义是什么呢?看2,3. 觉 ...
- Delphi是座宝山,有待挖掘
Delphi是座宝山,有待挖掘1. VCL源码是座宝山,把纷繁复杂的Windows编程封装到短短几个类里,不超过8000行代码,还额外包括许多其它的技巧2. RTL是座宝山,方便程序员使用底层运算,不 ...
- WebService另一种轻量级实现—Hessian 学习笔记
最近和同事聊天,得知他们在使用一种叫做Hessian的WebService实现方式实现远 程方法调用,是轻量级的,不依赖JavaEE容器,同时也是二进制数据格式传输,效率比SOAP的XML方式要高.感 ...
- O(1)时间内删除指定链表结点
题目 给定单链表头指针和一个结点指针,定义一个函数在O(1)时间内删除该结点. 分析 对于上图实例链表(a)删除指针p有两种方式 思路1:(b)找到前一个指针pre,赋值pre->next = ...
- HTML5塔防游戏——《三国塔防》 - Yorhom's Game Box
h3{ font-size:20px; } HTML5塔防游戏--<三国塔防> 游戏介绍: <三国塔防>是一款基于HTML5和Javascript的2D塔防游戏.游戏中除了塔防 ...
- 15_采用Pull解析器解析和生成XML内容
java还提供SAX和DOM用于解析XML Android还集成了Pull解析器——推荐 package cn.itcast.service; import java.io.InputStream; ...