【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 ...
随机推荐
- 在网页里插入flash的代码
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...
- python局部变量与全局变量
name = "head first python"def what_happens_here(): print(name) 1 name = "pytho ...
- 安装 Groovy
brew install groovy http://wiki.jikexueyuan.com/project/groovy-introduction/install-groovy.html
- MFC中 SDI/MDI框架各部分指针获取方式
VC MFC SDI/MDI框架各部分指针获取方式 整理总结一下,希望能帮助到别人. 获得CWinApp 获得CMainFrame 获得CChildFrame 获得CDocument 获得CV ...
- Mycat本地模式的自增长分表操作
Mycat对表t_rc_rule_monitor做分表操作 在mysql上执行(没有t_rc_rule_monitor) DROP TABLE IF EXISTS t_rc_rule_monitor; ...
- 【音乐App】—— Vue-music 项目学习笔记:歌手页面开发
前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 一.歌手页面布局与设计 需 ...
- 利用AFNetworking框架去管理从聚合数据上面请求到的数据
数据从JSON文档中读取处理的过程称为“解码”过程,即解析和读取过程,来看一下如果利用AFNetworking框架去管理从聚合数据上面请求到的数据. 一.下载并导入AFNetworking框架 这部分 ...
- LeetCode Recover Binary Search Tree——二查搜索树中两个节点错误
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing ...
- SAS连接MYSQL的步骤及引用数据表
1.建立逻辑库 libname dz ’物理路径'; 2.逻辑库做为桥梁连接SAS与MYSQL libname dz MYSQL USER=***** PASSWORD=**** DATABA ...
- VMware Workstation 永久许可证密钥
VMware是功能最强大的虚拟机软件,用户可在虚拟机同时运行各种操作系统,进行开发.测试.演示和部署软件,虚拟机中复制服务器.台式机和平板环境,每个虚拟机可分配多个处理器核心.主内存和显存. VMwa ...