【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 ...
随机推荐
- 【温故知新】——BABYLON.js学习之路·前辈经验(二)
前言:在上一篇随笔BABYLON.js学习之路·前辈经验(一)中回顾了组内同事们长时间在Babylon开发实践中的总结出的学习之路和经验,这一篇主要对开发中常见的一些功能点做一个梳理,这里只作为温故知 ...
- vim g s 替换区别
vim g s 替换区别 PS:一篇好文收藏备用,今天用它解决了一个大问题. 发信人: vale (浅谷), 信区: VIM标 题: global命令详解 发信站: 水木社区 (Fri Ju ...
- Win7如何修复开机画面
将下面文件保存为"修复Win7开机画面.bat"双击运行即可 bcdedit /set {current} locale zh-CN
- nodejs - 创建服务器(1)
在此之前,确保你已经安装了Node(并且你很会折腾) - 有人说,Java脚本和Java最本质的区别就是一个超会更新,一个死守旧. 如果你没有安装,请去官网下载并且安装:http://nodejs.c ...
- UNP学习笔记(第十八章 路由套接字)
路由套接字上支持3种类型的操作 1). 进程能通过写路由套接字向内核发消息. 2). 进程能通过路由套接字从内核读消息. 3). 进程可以用sysctl函数得到路由表或列出所有已配置的接口. 数据链路 ...
- Oracle 中session和processes的初始设置
http://blog.163.com/succu/blog/static/193917174201252911727149/ 1.sessions 在初始化参数所设定的限制中,最为人所知的估计就 ...
- SPA路由机制详解(看不懂不要钱~~)
前言 总所周知,随着前端应用的业务功能起来越复杂,用户对于使用体验的要求越来越高,单面(SPA)成为前端应用的主流形式.而大型单页应用最显著特点之一就是采用的前端路由跳转子页面系统,通过改变页面的UR ...
- js 组合键监听ctrl + enter
$(window).keydown(function (event) { // 监听esc键退出全屏 if (event.keyCode == 27) { } // 监听 Ctrl + Enter 可 ...
- ubuntu boost.python
安装boost(未尝试只安装 libboost-python-dev) sudo apt-get install libboost-all-dev 新建hello_ext.cpp,输入以下代码 1 c ...
- 解决mysql数据库乱码问题
MySQL的SQL语言是用于访问数据库的最常用标准化语言.MySQL软件采用了双授权政策,它分为社区版和商业版,由于其体积小.速 度快.总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选 ...