LeetCode——Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
写一个函数找出字符串数组中的最长共现前缀字符串。
思路:共现。即要求数组中的所有元素的前缀中都要出现。所以所得的结果肯定是最短字符串的部分或所有或都不是,总之要以最短字符串为基准与其它字符串比較。
public static String longestCommonPrefix(String[] strs){
int len = strs.length;
if(len <= 0)
return "";
if(len == 1)
return strs[0];
int minlen = strs[0].length();
String temp = strs[0];
//找出一个长度最短的字符串
for(int i=1;i<len;i++){
if(strs[i].length() < minlen){
minlen = strs[i].length();
temp = strs[i];
}
}
int i=0,j = 0;
StringBuffer buf = new StringBuffer();
while(j < temp.length()){
for(i=0;i<strs.length;i++){
if(strs[i].charAt(j) != temp.charAt(j))
break;
}
if(i < len)
break;
buf.append(temp.charAt(j));
j++;
}
return buf.toString();
}
LeetCode——Longest Common Prefix的更多相关文章
- [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. class Solutio ...
- Leetcode::Longest Common Prefix && Search for a Range
一次总结两道题,两道题目都比较基础 Description:Write a function to find the longest common prefix string amongst an a ...
- 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. Hide Tags Str ...
- [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀
题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...
- LeetCode Longest Common Prefix 最长公共前缀
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
- leetcode Longest Common Prefix 多个字符串的最长字串
public class Solution { public String get(String a,String b) { if(a==""||b=="") ...
- leetcode Longest Common Prefix python
class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str ...
随机推荐
- vi 编辑器的日常使用
命令行模式: 光标管理 text 屏幕 行 单词 gg 跳转到文档头部 H 跳转到屏幕首行 ^ 或 数字0 跳转到行首 w 向前 G 跳转到文档尾部 M 跳转到屏幕中行 $ 跳转到行尾 b 向后 ...
- V4L2驱动程序架构
1 V4L2简介 video4linux2(V4L2)是Linux内核中关于视频设备的内核驱动,它为Linux中视频设备访问提供了通用接口,在Linux系统中,V4L2驱动的Video设备节点路径通常 ...
- 2019-03-18 OpenCV Tesseract-OCR 下载 安装 配置(cv2 报错)
OpenCV 下载 安装 配置 1.下载和Python版本对应的版本,此为下载地址 2.安装(在powershell管理员模式下安装) pip3 install .\opencv_python-3.4 ...
- mybatis-plus注解版实现多表联查(sql)
mybatis注解版实现多表联查 需求: 用户有角色,角色有权限,需要一次取用户信息包含角色信息及其对应权限 实体类: package cn.zytao.taosir.common.model.use ...
- snprintf
snprintf(),函数原型为int snprintf(char *str, size_t size, const char *format, ...). 将可变参数 “…” 按照format的 ...
- 洛谷 P2652 同花顺
P2652 同花顺 题目背景 所谓同花顺,就是指一些扑克牌,它们花色相同,并且数字连续. 题目描述 现在我手里有n张扑克牌,但它们可能并不能凑成同花顺.我现在想知道,最少更换其中的多少张牌,我能让这 ...
- Linux 网络搭建
如果系统环境崩溃. 调用/usr/bin/vim /etc/profile Windows 1 本地连接使用固定IP vmware 8 2 修改Windows的hosts地址 ...
- Xdoclet + Ant自己主动生成Hibernate配置文件
在使用Hibernate的时候,过多的Hibernate配置文件是一个让人头疼的问题. 近期接触了Xdoclet这个工具. 它实际上就是一个自己主动代码生成的工具.Xdoclet不能单独执行,必须搭配 ...
- [CSS3] Responsive Table -- no more table
When the screen size is small, we can use "no more table" solution. So instead of render t ...
- 同一个TextView设置不同的颜色和大小
//strategy1是一个TextView SpannableStringBuilder builder1 = new SpannableStringBuilder(strategy1.getTex ...