LeetCode--No.014 Longest Common Prefix
14. Longest Common Prefix
- Total Accepted: 112204
- Total Submissions: 385070
- Difficulty: Easy
Write a function to find the longest common prefix string amongst an array of strings.
思路:
题目很简单,求字符串数组的最长的共同前缀。也没什么思路,诸位比较呗,代码如下:
public class No_014 {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0){
return "" ;
}
StringBuilder res = new StringBuilder() ;
//通过flag标志位来判断是否结束循环,以及是否应该加入返回的结果中
boolean flag = true ;
int index = 0 ;
while(flag){
char ch ;
if(index < strs[0].length()){
ch = strs[0].charAt(index) ;
}else{
flag = false ;
continue ;
}
for(int i = 1 ; i < strs.length ; i++){
if(index < strs[i].length() && strs[i].charAt(index) == ch){
continue ;
}else{
flag = false ;
break ;
}
}
//通过判断flag的值来判断是否是满足所有条件的
if(flag){
res.append(ch) ;
index++ ;
}
}
return res.toString() ;
}
}
LeetCode--No.014 Longest Common Prefix的更多相关文章
- 【LeetCode】014. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 题解: 简单的暴力遍历解决 ...
- 《LeetBook》leetcode题解(14):Longest Common Prefix[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【JAVA、C++】LeetCode 014 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【一天一道LeetCode】#14 Longest Common Prefix
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- LeetCode题解(14)--Longest Common Prefix
https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...
- # Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- No.014 Longest Common Prefix
14. Longest Common Prefix Total Accepted: 112204 Total Submissions: 385070 Difficulty: Easy Write a ...
- 【LeetCode】14. Longest Common Prefix 最长前缀子串
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...
随机推荐
- windows 远程连接“发生身份验证错误 要求的函数不受支持”
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\P ...
- 43 【redis cluster】
有两篇文章不错,可以看下: 1,初步理解redis cluster:https://blog.csdn.net/dc_726/article/details/48552531 2,仔细理解redis ...
- ubuntu输入法
一直都是使用搜狗的,但是感觉一直都不兼容.经常输入一串文字导致输入法崩溃 比如 ‘外显’waixian就崩了. 而且会出现fcitx 100%cpu占用的情况 pkill fcitx &&am ...
- 20170805_linux
http://blog.csdn.net/aaaaatiger/archive/2007/07/28/1713611.aspx Delphi/Pascal code ? 1 2 3 4 5 6 7 ...
- 《Linux就该这么学》第九天课程
这次课程主要学了如何分区以及RAID技术 有扩展分区:扩展分区可以指定sdb*中的*(1~4)为多少,则其他三个为主分区,扩展分区中的逻辑分区sdb*(*从5开始)无扩展分区:四个皆为主分区 RAID ...
- linux关机、重启命令
1.shutdown -h 10 //计算机将在10分钟后关机,且会显示在登录用户的当前屏幕中 2.shutdown -h now //立即关机 3.shutdown -h 20:25 //系统会在2 ...
- ABP框架系列之二十五:(Embedded-Resource-Files-嵌入式资源文件)
Introduction ASP.NET Boilerplate provides an easy way of using embedded Razor views (.cshtml files) ...
- virtual关键字
出于多态的考虑,为了覆盖, 子类同名覆盖函数(函数名.参数.返回值都相同) virtual void print(): 这也许会使人联想到函数的重载,但稍加对比就会发现两者是完全不同的:(1)重载的几 ...
- 第一节20181109 《Linux就该这么学》
在网上发现了刘老师的linux课程,关注了一段时间感觉很好就在10月1日活动日报了第19期的班,希望自己能坚持下来学好linux.
- Altera 在线资源使用
Altera 在线资源使用 Altera 在线资源使用 1 1.Altera中文版 2 2.建立myaltera账户 获取官网信息与支持 2 3系统化的设计资源 2 3.1.设计实例 2 3.2.参考 ...