当前位置:网站首页>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);
}
}
边栏推荐
- 如何设计 API 接口,实现统一格式返回?
- Time complexity & space complexity
- MySQL数据库(基础篇)
- Common Oracle SQL statements
- 食堂用户菜品关系系统(C语言课设)
- 全国气象数据/降雨量分布数据/太阳辐射数据/NPP净初级生产力数据/植被覆盖度数据
- R language principal component PCA, factor analysis, clustering analysis of regional economy analysis of Chongqing Economic Indicators
- namespace基础介绍
- offer如何选择该考虑哪些因素
- Pointer and array are input in function to realize reverse order output
猜你喜欢
Why do many people misunderstand technical debt
If you‘re running pod install manually, make sure flutter pub get is executed first.
AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
- [email protected]映射关系问题"/>
接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
JS variable plus
Ansible reports an error: "MSG": "invalid/incorrect password: permission denied, please try again“
Decorator basic learning 02
九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
随机推荐
Meow, come, come: do you really know if, if else
What is Web3
Monitoring cannot be started after Oracle modifies the computer name
Vscode 如何使用内置浏览器?
STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
sscanf,sscanf_ S and its related usage "suggested collection"
Section 1: (3) logic chip process substrate selection
Jetson nano配置pytorch深度学习环境//待完善
Lessons and thoughts of the first SQL injection
Why do many people misunderstand technical debt
JS variable case output user name
[practice leads to truth] is the introduction of import and require really the same as what is said on the Internet
U++ 游戏类 学习笔记
A picture to understand! Why did the school teach you coding but still not
Tree map: tree view - draw covid-19 array diagram
A line of R code draws the population pyramid
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
acwing 843. n-皇后问题
3GPP信道模型路损基础知识
关于01背包个人的一些理解