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. 思路:求最长前缀子 ...
随机推荐
- xml文件中&符号需要转义为&
xml文件中&符号需要转义为&
- DJango 基础 (3)
模板路径 在配置文件setting.py中找到TEMPLATES设置来配置. 这是一个设置选项的列表,模板大都包含两项通用设置:两种方式配置模板: 第一种: DIRS 定义一个目录列表,模板引擎按列表 ...
- matplotlib 坑
1 archlinux里安装好matplotlib之后一定要安装python-cario pacman -S python-cairo
- windows 远程连接“发生身份验证错误 要求的函数不受支持”
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\P ...
- [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [leetcode]31. Next Permutation下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- SpringAOP日志配置
SpringAOP日志配置 配置文件配置 l 配置spring-mvc.xml <aop:config proxy-target-class="true" /> &l ...
- 一个JAVA程序员经常访问的网站
综合技术网站: CSDN http://www.csdn.net/ 51CTO http://www.51cto.com/ 开源中国社区 http:/ ...
- printf 字符串格式化
在将各种类型的数据构造成字符串时,sprintf 的强大功能很少会让你失望.由于sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已,前者打印到字符串中,后者则直接在命令行上输出 ...
- [快速幂][NOIP2012]转圈游戏
转圈游戏 题目描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置, ...