LeetCode 14: Longest Common Prefix
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
求最长公共前缀。
代码例如以下:
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int length = strs.size();
if (length <= 0)
return "";
string prefix = strs[0];
int i, j;
for (i=1; i<length; i++)
{
string tmpStr = strs[i];
if (prefix.length()==0 || tmpStr.length()==0)
return "";
int len = min(prefix.length(), tmpStr.length());
for (j=0; j<len; j++)
{
if (prefix[j] != tmpStr[j])
break;
}
prefix = prefix.substr(0,j);
}
return prefix;
}
};
LeetCode 14: 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 OJ:Longest Common Prefix(最长公共前缀)
Write a function to find the longest common prefix string amongst an array of strings. 求很多string的公共前 ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- 【LeetCode】LeetCode——第14题:Longest Common Prefix
14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Sub ...
- LeetCode第[14]题(Java): Longest Common Prefix
题目:最长公共前缀 难度:EASY 题目内容: Write a function to find the longest common prefix string amongst an array o ...
- Leetcode算法刷题:第14题 Longest Common Prefix
Longest Common Prefix 题目 给予一个列表,元素为字符串,写一个程序找出最长公共前缀 解题思路 先比较两个字符串,如果第一个字符不一样,则返回空值,比较完成后,用这个公共字符串和下 ...
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- 【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第14题--Longest Common Prefix
Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...
随机推荐
- 刷题总结——Aeroplane chess(hdu4405)
题目: Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids labele ...
- hibernate用配置文件的方式实现orm
本文主要讲用配置文件的方式讲如何把一个对象和数据库中的表关联起来,其实用配置文件本质是和用注解的方式是一样的. 思路:1.写一个domain对象(如Person.java) 2.写这个domain对象 ...
- Topcoder SRM 603 div1题解
昨天刚打了一场codeforces...困死了...不过赶在睡前终于做完了- 话说这好像是我第一次做250-500-1000的标配耶--- Easy(250pts): 题目大意:有一棵树,一共n个节点 ...
- 2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest(solved 8/11)
这套题似乎是省选前做的,一直没来写题解---补上补上>_< 链接:http://codeforces.com/gym/101147 一样先放上惨不忍睹的成绩好了--- Problem A ...
- SpringMVC+MyBatis+Shiro 配置文件详解
1.web.xml文件的配置 <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...
- Js String 属性扩展
String.prototype.startsWith = function (startStr) { var d = startStr.length; return (d >= 0 &am ...
- 多个类的DLL封装及调用
#define FaceLIBDLL #include "stdafx.h" #include "facedll.h" #include <opencv2 ...
- springMVC 配置中易犯的小错误
搭建springMVC环境时有可能遇到:'警告: No mapping found for HTTP request with URI [/WEB-INF/pages/helloWorld.jsp] ...
- 洛谷——P1579 哥德巴赫猜想(升级版)
P1579 哥德巴赫猜想(升级版) 题目背景 1742年6月7日哥德巴赫写信给当时的大数学家欧拉,正式提出了以下的猜想:任何一个大于9的奇数都可以表示成3个质数之和.质数是指除了1和本身之外没有其他约 ...
- Spark-submit脚本解读
#!/usr/bin/env bash # # Licensed to the Apache Software Foundation (ASF) under one or more # contrib ...