当前位置:网站首页>Task19_14_最长公共前缀
Task19_14_最长公共前缀
2022-08-08 23:58:00 【weixin_961876584】
题目
- 编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 “”。
思路
- 把第一个字符串作为基准
- 纵向对比
- 取第一个字符串的子串
public class Solution
{
public string LongestCommonPrefix(string[] strs)
{
if (strs.Length == 0) return "";
for (int i = 0; i < strs[0].Length; i++)
{
for (int j = 1; j < strs.Length; j++)
{
if (i>=strs[j].Length|| strs[j][i] != strs[0][i])
return strs[0].Substring(0,i);
}
}
return strs[0];
}
}
边栏推荐
猜你喜欢
随机推荐
ImportError: cannot import name downsample
最常用正则表达式
第三章 传输层
第四章 SQL与关系数据库基本操作(上)
win10:如何修改以太网名字
According to the address returned by Baidu Map, intercept the province, city, district
04 Spark on 读取外部数据分区策略(源码角度分析)
C#中构造函数的作用
关于vm虚拟机虚拟网络已禁用
Get the start time of the week
多种决策树及应用 笔记
MySQL导入导出数据库
OSPF小实验
如何使用电脑云盘?
Intelligent search baidu map and drag the picture circle
开源协议(Open Source License)
测试用例的原则、缺陷报告怎么写你都知道吗?
C#未将对象引用设置到对象的实例
Common problems in installing mysql in linux environment and using it
JS基础知识