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() ;
}
}
No.014 Longest Common Prefix的更多相关文章
- 【JAVA、C++】LeetCode 014 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...
- LeetCode--No.014 Longest Common Prefix
14. Longest Common Prefix Total Accepted: 112204 Total Submissions: 385070 Difficulty: Easy Write a ...
- 【LeetCode】014. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 题解: 简单的暴力遍历解决 ...
- [Leetcode]014. Longest Common Prefix
public class Solution { public String longestCommonPrefix(String[] strs) { if(strs == null || strs.l ...
- 014 Longest Common Prefix 查找字符串数组中最长的公共前缀字符串
编写一个函数来查找字符串数组中最长的公共前缀字符串. 详见:https://leetcode.com/problems/longest-common-prefix/description/ 实现语言: ...
- 《LeetBook》leetcode题解(14):Longest Common Prefix[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- No.014:Longest Common Prefix
问题: Write a function to find the longest common prefix string amongst an array of strings. 官方难度: Eas ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
随机推荐
- php中 -> 和 => 和 :: 的用法 以及 self 和 $this 的用法
=> 数组中 用于数组的 key 和 value之间的关系例如:$a = array( '0' => '1', '2' => '4',); echo $a['0'];echo $a[ ...
- Java与数据库之间时间的处理
Java与数据库之间时间的处理 在数据库中建表: DROP TABLE IF EXISTS `times`; CREATE TABLE `times` ( `id` int(11) NOT NULL ...
- 在MacOX下安装python-opencv
下载好opencv之后 1. 在文件夹下新建一个release或build的文件夹: 2. cmake . make 3.在该build文件夹下 nano .bash_profile 把python的 ...
- Hadoop 如何查看是否32位
1.从哪些地方可以识别hadoop是32位还是64位?2.hadoop本地库在什么位置? hadoop在安装的时候,我们需要知道hadoop版本是32位还是64位. hadoop官网本来提供的都是32 ...
- HOW TO BE SINGLE 最后那段的摘录
我一直在思考我们不得不单身的时间这个时间我们需要擅长一个人独处但是有多少独处的状态是我们想要拥有的呢难道不是件很危险的事情吗当你适应状态并且如鱼得水的时候所以当你安定下来 你就会与某人擦肩而过吗 有些 ...
- PAT (Basic Level) Practise:1028. 人口普查
[题目链接] 某城镇进行人口普查,得到了全体居民的生日.现请你写个程序,找出镇上最年长和最年轻的人. 这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人,而今天是 ...
- HDfs命令
HDFS命令分为用户命令(dfs,fsck等),管理员命令(dfsadmn,namenode,datanode等) hdfs -ls -lsr 执行lsr 是递归显示 drwxr-xr-x -hado ...
- linux服务之maven
curl -O http://mirrors.noc.im/apache/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.zip [root@d ...
- [Hibernate] - Generic Dao
使用泛型写了一个通用的Hibernate DAO类. GenericDao接口 package com.my.dao; import java.io.Serializable; import java ...
- 6、java中的构造代码块
/* 演示构造代码块的应用 */ class Person { String name; int age; //构造代码块 { cry(); } Person(String name, int age ...