[LeetCode]14. Longest Common Prefix最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Note:
All given inputs are in lowercase letters a-z.
求几个字符串的公共前缀,两种方法
(1)暴力
利用两层循环,先取出第一个字符串的第i个字符,然后比较s[1]-s[n]的第i个字符是否相等
public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) return "";
String res ="";
for (int i = 0; i < strs[0].length(); i++) {
char c = strs[0].charAt(i);
for (int j = 1; j < strs.length; j++) {
if (i >= strs[j].length() || strs[j].charAt(i) != c) {
return res;
}
}
res =res+c;
}
return res;
}
}
(2)排序后比较首尾
字符串比较的结果,按照首个不同字符的大小排序,比如ab就排在ac的前面
["flower","flow","flight"]比较的结果是[flight,flow,flower],我们统计首尾,fl是最长前缀
["dog","racecar","car"]比较的结果是[car,dog,racecar]
根据这个特性,假如存在最长公共前缀,s[0]和s[n]之间的最长公共前缀就是我们要求的结果,因为排序结果是按照第一个非公共字符开始的
这里我们把过程简化到只要比较首尾字符,而不用比较所有的
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) return "";
Arrays.sort(strs);
int i = 0, len = Math.min(strs[0].length(), strs[strs.length - 1].length());
while (i < len && strs[0].charAt(i) == strs[strs.length - 1].charAt(i)) i++;
return strs[0].substring(0, i);
}
}
[LeetCode]14. Longest Common Prefix最长公共前缀的更多相关文章
- [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 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- 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] 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(最长公共前缀)
这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...
- # Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...
- LeetCode Longest Common Prefix 最长公共前缀
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
- Longest Common Prefix -最长公共前缀
问题:链接 Write a function to find the longest common prefix string amongst an array of strings. 解答: 注意 ...
- Leetcode 14. Longest Common Prefix(水)
14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...
随机推荐
- json互转笔记
JSON.parse()与JSON.stringify()的区别 JSON.parse()[从一个字符串中解析出json对象] 例子: //定义一个字符串 var data='{"name& ...
- STL_ALGORITHM_H
sort_unique_copy /////////////////////////////////////////////////////////// // Copyright (c) 2013, ...
- 区块链中的密码学(二)-RSA算法分析和实现
密码学领域中,加密算法主要分为对称加密和非对称加密,随着信息时代安全性要求越来越高,对称加密因为其易被破解的原因逐渐被舍弃.而RSA算法是目前密码学世界中比较流行的非对称加密算法,命名是根据其发明者R ...
- CGContext ----生成图片--image
//获取图片 + (UIImage*) getImageWithColor:(UIColor*)color andHeight:(CGFloat)height { CGRect r= CGRectMa ...
- iperf简单说明
1.官网下载安装包 https://iperf.fr/iperf-download.php 2.安装(根据下载包的类型进行安装) 3.参数说明服务端(默认监听端口5201) iperf -s -u - ...
- hdu3483 A Very Simple Problem 非线性递推方程2 矩阵快速幂
题目传送门 题目描述:给出n,x,mod.求s[n]. s[n]=s[n-1]+(x^n)*(n^x)%mod; 思路:这道题是hdu5950的进阶版.大家可以看这篇博客hdu5950题解. 由于n很 ...
- 浅谈关于树形dp求树的直径问题
在一个有n个节点,n-1条无向边的无向图中,求图中最远两个节点的距离,那么将这个图看做一棵无根树,要求的即是树的直径. 求树的直径主要有两种方法:树形dp和两次bfs/dfs,因为我太菜了不会写后者这 ...
- day23 模块
1. 模块 1. 首先,我们先看个老生常谈的问题. 什么是模块. 模块就是一个包含了python定义和声 明的文件, 文件名就是模块的名字加上.py后缀. 换句话说我们目前写的所有的py文件都可以 看 ...
- LeeCode(No4 - Median of Two Sorted Arrays)
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- python3 sorted()函数笔记
import randoma=[]for i in range(9): b=random.randint(0,9)#生产9个随机数 a.append(b)#把生成的随机数添加到列表里面print(so ...