当前位置:网站首页>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 notes
- Programmers go to work fishing, so play high-end!
- R语言主成分pca、因子分析、聚类对地区经济研究分析重庆市经济指标
- 树与图的深度优先遍历模版原理
- 装饰器基础学习02
- 架构实战训练营|课后作业|模块 6
- STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
- sscanf,sscanf_ S and its related usage "suggested collection"
- Introduction to the PureMVC series
- JS variable case output user name
猜你喜欢
3GPP信道模型路损基础知识
Vscode 如何使用内置浏览器?
Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)
A detailed explanation of head pose estimation [collect good articles]
为什么很多人对技术债务产生误解
U++ 游戏类 学习笔记
Ansible中的inventory主機清單(預祝你我有數不盡的鮮花和浪漫)
Time complexity & space complexity
【愚公系列】2022年7月 Go教学课程 005-变量
Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
随机推荐
leetcode 53. Maximum Subarray 最大子数组和(中等)
Windows are not cheap things
STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
动态生成表格
Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
Common Oracle SQL statements
A line of R code draws the population pyramid
Thesis landing strategy | how to get started quickly in academic thesis writing
STM32F103 realize IAP online upgrade application
What work items do programmers hate most in their daily work?
[Android kotlin collaboration] use coroutinecontext to realize the retry logic after a network request fails
STM32F103实现IAP在线升级应用程序
Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
JS variable
Field data acquisition and edge calculation scheme of CNC machine tools
JS input and output
窗口可不是什么便宜的东西