当前位置:网站首页>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);
}
}
边栏推荐
- [Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
- [hand torn STL] list
- Appium practice | make the test faster, more stable and more reliable (I): slice test
- 深入解析Kubebuilder
- 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
- ServiceMesh主要解决的三大痛点
- 窗口可不是什么便宜的东西
- Is there any way to bookmark the code in the visual studio project- Is there a way to bookmark code in a Visual Studio project?
- Oracle -- 视图与序列
- Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
猜你喜欢
JS also exports Excel
Meow, come, come: do you really know if, if else
offer如何选择该考虑哪些因素
How does vscade use the built-in browser?
Decorator basic learning 02
Section 1: (3) logic chip process substrate selection
Ansible reports an error: "MSG": "invalid/incorrect password: permission denied, please try again“
Inventory host list in ansible (I wish you countless flowers and romance)
Read of shell internal value command
Depth first traversal template principle of tree and graph
随机推荐
Analyse approfondie de kubebuilder
Section 1: (3) logic chip process substrate selection
Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
Acl2022 | decomposed meta learning small sample named entity recognition
Thesis landing strategy | how to get started quickly in academic thesis writing
JDBC link Oracle reference code
日常工作中程序员最讨厌哪些工作事项?
npm ERR! 400 Bad Request - PUT xxx - “devDependencies“ dep “xx“ is not a valid dependency name
offer如何选择该考虑哪些因素
leetcode 53. Maximum subarray maximum subarray sum (medium)
A picture to understand! Why did the school teach you coding but still not
【愚公系列】2022年7月 Go教学课程 005-变量
Read of shell internal value command
Detect when a tab bar item is pressed
Flex layout and usage
[hand torn STL] list
R语言主成分pca、因子分析、聚类对地区经济研究分析重庆市经济指标
mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi
史上最全学习率调整策略lr_scheduler
acwing 843. n-皇后问题