当前位置:网站首页>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);
}
}
边栏推荐
- U++ 元数据说明符 学习笔记
- How to package the parsed Excel data into objects and write this object set into the database?
- R descriptive statistics and hypothesis testing
- Gpt-3 is a peer review online when it has been submitted for its own research
- offer如何选择该考虑哪些因素
- 【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析
- Jetson nano configures pytorch deep learning environment / / to be improved
- 【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
- Advertising attribution: how to measure the value of buying volume?
- Pointer and array are input in function to realize reverse order output
猜你喜欢
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
U++ 元数据说明符 学习笔记
【实践出真理】import和require的引入方式真的和网上说的一样吗
Meow, come, come: do you really know if, if else
Lessons and thoughts of the first SQL injection
Decorator basic learning 02
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
Tree map: tree view - draw covid-19 array diagram
Introduction to namespace Basics
Basic knowledge of road loss of 3GPP channel model
随机推荐
If you ask me about R code debugging, I will tell you head, STR, help
mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi
U++ 元数据说明符 学习笔记
一文搞懂常见的网络I/O模型
Common Oracle SQL statements
STM32 encapsulates the one key configuration function of esp8266: realize the switching between AP mode and sta mode, and the creation of server and client
Acl2022 | decomposed meta learning small sample named entity recognition
[Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
3.基金的类型
Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
A detailed explanation of head pose estimation [collect good articles]
【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
Tree map: tree view - draw covid-19 array diagram
PLC Analog output analog output FB analog2nda (Mitsubishi FX3U)
Error: No named parameter with the name ‘foregroundColor‘
What is JVM? What are the purposes of JVM tuning?
Markdown editor
Why do many people misunderstand technical debt
Jetson nano配置pytorch深度学习环境//待完善
深入解析Kubebuilder