【Leetcode-easy】Longest Common Prefix
思路:每次从字符数组中读取两个字符串比较。需要注意输入字符串为空,等细节。
public String longestCommonPrefix(String[] strs) {
if(strs==null||strs.length==0){
return "";
}
int count=strs[0].length();
for(int i=1;i<strs.length;i++){
String str1=strs[i-1];
String str2=strs[i];
int len=Math.min(str1.length(),str2.length());
if(len<count){
count=len;
}
int comNum=0;
for(int j=0;j<count;j++){
if(str1.charAt(j)==str2.charAt(j)){
comNum++;
}else{
break;
}
}
if(comNum<count){
count=comNum;
}
}
if(count==0){
return "";
}
return strs[0].substring(0, count);
}
【Leetcode-easy】Longest Common Prefix的更多相关文章
- leetcode【14题】Longest Common Prefix
题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...
- C# 写 LeetCode easy #14 Longest Common Prefix
14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- 【leetcode】Longest Common Prefix (easy)
Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- 【LeetCode算法-14】Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- 【LeetCode每天一题】Longest Common Prefix(最长前缀)
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- 【LeetCode】 Longest Common Prefix
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- 【leetcode刷题笔记】Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 题解:以strs[0]为模 ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
随机推荐
- MFC中 用Static控件做超链接(可以实现变手形、下划线、字体变色等功能)
1.新建一个基于对话框的工程MyLink,在对话框中拖一个Static控件,ID可改为IDC_SLink. 2.在头文件中添加成员变量: private: CRect m_Rect; CFont* m ...
- 【温故知新】——Bootstrap响应式知识点复习
前言:本文是自己在学习课程中的课程笔记,这里用来温故知新的,并非本人原创. 开发工具 1.记事本,Editplus,... ... 2.Sublime,Dreamweaver 3.Webstorm = ...
- 2017.2.13 开涛shiro教程-第十二章-与Spring集成(一)配置文件详解
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(一)配置文件详解 1.pom.xml ...
- 雕刻效果的实现【OpenCV+QT】
雕刻能够区分为凸雕和凹雕. 凸雕基右下角的点减去左上角的点. 凹雕是左上角的点减去右下角的点. [效果图] 由于进行了缩放.效果看起来差一些.
- 【Python】学习笔记十五:循环对象
循环对象 所谓的循环对象,包含有一个next()方法(python3中为__next__() ),这个方法的目的就是进行到下一个结果,而在结束一系列结果之后,举出StopIteration错误 当一个 ...
- IOS Audio开发集合
打算每天抽出一点时间学习音频方面的知识,在此做下汇总: 1. 多媒体层预览 根据结构,明确学习内容.
- Linux是怎么启动的(整理)
昨天笔试考了一道关于linux系统启动的过程,当时没答上来,现在整理出来(其实并不复杂). 按下电源按钮的直到欢迎页出来之后,linux总共做的事可以分为五步来完成. 1. BIOS加电自检: 加电 ...
- eclipse右下角总显示自动下载xml和jar,如何关闭
windows->preference->查找download-选择Models->右侧下方Enable auto-download去掉 2.Automatic Updates-&g ...
- PCA的数学原理Matlab演示
关于 PCA(Principal component analysis)主成分分析.是SVD(Singular value decomposition)神秘值分析的一种特殊情况.主要用于数据降维.特征 ...
- C语言批量数据到动态二维数组
上一篇文章将文件读取放到静态创建的二维数组中,可是结合网络上感觉到今天的DT时代,这样批量大量读取一个上百行的数据,分配的内存是否可能由于大量的数据而产生溢出呢,近期一直研究里malloc函数.通过它 ...