当前位置:网站首页>Leetcode longest public prefix
Leetcode longest public prefix
2022-07-07 05:00:00 【kt1776133839】
Title Description :
Write a function to find the longest common prefix in the string array .
If no common prefix exists , Returns an empty string "".
Examples :
Example 1:
Input :strs = ["flower","flow","flight"]
Output :"fl"
Example 2:
Input :strs = ["dog","racecar","car"]
Output :""
explain : Input does not have a common prefix .
Tips :
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] It's only made up of lowercase letters
Java Program :
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
String prefix = strs[0];
int count = strs.length;
for (int i = 1; i < count; i++) {
prefix = longestCommonPrefix(prefix, strs[i]);
if (prefix.length() == 0) {
break;
}
}
return prefix;
}
public String longestCommonPrefix(String str1, String str2) {
int length = Math.min(str1.length(), str2.length());
int index = 0;
while (index < length && str1.charAt(index) == str2.charAt(index)) {
index++;
}
return str1.substring(0, index);
}
}边栏推荐
- leetcode 53. Maximum subarray maximum subarray sum (medium)
- Monitoring cannot be started after Oracle modifies the computer name
- Using thread class and runnable interface to realize the difference between multithreading
- 关于01背包个人的一些理解
- Wechat can play the trumpet. Pinduoduo was found guilty of infringement. The shipment of byte VR equipment ranks second in the world. Today, more big news is here
- Code source de la fonction [analogique numérique] MATLAB allcycles () (non disponible avant 2021a)
- National meteorological data / rainfall distribution data / solar radiation data /npp net primary productivity data / vegetation coverage data
- Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
- MySQL数据库(基础篇)
- 装饰器基础学习02
猜你喜欢

Function pointer and pointer function in C language
![[hand torn STL] list](/img/aa/7060ab20b41936419041067cf9daed.jpg)
[hand torn STL] list

JS also exports Excel

程序员上班摸鱼,这么玩才高端!

为什么很多人对技术债务产生误解

Vscode 如何使用内置浏览器?

How to package the parsed Excel data into objects and write this object set into the database?

Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str

How does vscade use the built-in browser?
![Local tool [Navicat] connects to remote [MySQL] operation](/img/e8/a7533bac4a70ab5aa3fe15f9b0fcb0.jpg)
Local tool [Navicat] connects to remote [MySQL] operation
随机推荐
Meow, come, come: do you really know if, if else
Oracle - views and sequences
关于01背包个人的一些理解
Code source de la fonction [analogique numérique] MATLAB allcycles () (non disponible avant 2021a)
STM32F103实现IAP在线升级应用程序
namespace基础介绍
Acl2022 | decomposed meta learning small sample named entity recognition
Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
R language principal component PCA, factor analysis, clustering analysis of regional economy analysis of Chongqing Economic Indicators
Local tool [Navicat] connects to remote [MySQL] operation
Vscode automatically adds a semicolon and jumps to the next line
Poor math students who once dropped out of school won the fields award this year
Flask项目使用flask-socketio异常:TypeError: function() argument 1 must be code, not str
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
Advertising attribution: how to measure the value of buying volume?
为什么很多人对技术债务产生误解
Introduction to the PureMVC series
Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
01 machine learning related regulations