78. Longest Common Prefix【medium】
Given k strings, find the longest common prefix (LCP).
For strings "ABCD", "ABEF" and "ACEF", the LCP is "A"
For strings "ABCDEFG", "ABCEFG" and "ABCEFA", the LCP is "ABC"
解法一:
class Solution {
public:
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
int min(int a, int b) {
return ((a < b) ? a : b);
}
int calCount(string & a, string & b) {
int la = a.size();
int lb = b.size();
int count = ;
for (int i = ; i < la && i < lb; ++i) {
if (a[i] == b[i]) {
count++;
} else {
return count;
}
}
}
string longestCommonPrefix(vector<string> &strs) {
int count = INT_MAX;
int size = strs.size();
if (size == ) {
return "";
}
for (int i = ; i < strs.size() - ; ++i) {
count = min(calCount(strs[i], strs[i + ]), count);
}
return strs[].substr(, count);
}
};
解法二:
class Solution {
public:
/**
* @param strs: A list of strings
* @return: The longest common prefix
*/
string longestCommonPrefix(vector<string> &strs) {
if (strs.size() == ) {
return "";
}
string prefix = "";
for (int i = ; i < strs[].length(); i++) {
for (int j = ; j < strs.size(); j++) {
if (strs[j][i] != strs[][i]) {
return prefix;
}
}
prefix += strs[][i];
}
return prefix;
}
};
78. Longest Common Prefix【medium】的更多相关文章
- 14. Longest Common Prefix【leetcode】
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- LintCode 78:Longest Common Prefix
public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...
- leetcode:Longest Common Prefix【Python版】
1.当strs为空,直接输出“” 2.当strs中含有“”,直接输出“” 3.strs[0]的最长长度由最短公共长度l决定(code line:15) class Solution: # @retur ...
- spoj LCS2 - Longest Common Substring II && LCS - Longest Common Substring【SAM】
多串LCS很适合SA但是我要学SAM 对第一个串求SAM,然后把剩下的串在SAM上跑,也就是维护p和len,到一个点,如果有ch[p][c],就p=ch[p][c],len++,否则向fa找最下的有c ...
- 【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. 思路:求最长前缀子 ...
- 【Longest Common Prefix】cpp
题目: Write a function to find the longest common prefix string amongst an array of strings. 代码: class ...
随机推荐
- OpenCV图像处理篇之阈值操作函数
阈值操作类型 这5种阈值操作类型保留opencv tutorials中的英文名称.依次为: Threshold Binary:即二值化,将大于阈值的灰度值设为最大灰度值.小于阈值的值设为0. Thre ...
- webpack 处理CSS
1.安装插件 npm i style-loader css-loader --save-dev npm i postcss-loader --save-dev npm i autoprefixer - ...
- git查看各个branch之间的关系图
两种方法: 一. 使用Git log命令 git log --graph --decorate --oneline --simplify-by-decoration --all 说明: --deco ...
- python之smtplib模块 发送邮件
# -*- coding: utf-8 -*- #python 27 #xiaodeng #smtplib模块 发送邮件 import smtplib from email.mime.text imp ...
- spring aop的两种写法aspect和advisor
本文转自:https://www.cnblogs.com/leiOOlei/p/3709607.html 首先看个例子,如下 接口代码: package com.lei.demo.aop.schema ...
- oper
package main.java.com.zte.controller.ems; import java.util.HashMap; import java.util.List; import ja ...
- Android网络开发之WIFI
WIFI全称Wireless Fidelity, 又称802.11b标准.WIFI联盟成立于1999年,当时的名称叫做Wireless Ethernet Compatibility Alliance( ...
- 【laravel5.4+vue.js】laravel 循环三维数组,解决:htmlentities() expects parameter 1 to be string, array given
laravel循环三维数组 +++ vue.js循环三维数组 (数据均是以三维数组形式存在的) <form-item label="权限名称" prop=" ...
- awk打印倒数第2列
cat 1-iplist.txt | awk '{ print $(NF-2) }'|wc 实际示例: 打印nginx日志中 变量request_time超过3秒的日志信息 [root@datalin ...
- iOS开发-Objective-C Block的实现方式
前言:我们可以把Block当作一个闭包函数,它可以访问外部变量和局部变量,但默认是不可以修改外部变量.你可以使用它来做回调方法,比起使用代理(Delegate)会更加直观.顺带一提,苹果很多的接口(A ...