leetcode Longest Common Prefix 多个字符串的最长字串
public class Solution {
public String get(String a,String b)
{
if(a==""||b=="") return "";
int len1=a.length();
int len2=b.length();
int len=len1;
if(len>=len2) len=len2;
String s="";
for(int i=0;i<len;i++)
{
if(a.charAt(i)==b.charAt(i))
{
s+=a.charAt(i);
}
else break;
}
return s;
}
public String longestCommonPrefix(String[] strs) {
int len=strs.length;
if(len==0) return "";
if(len==1) return strs[0];
String ans=get(strs[0],strs[1]);
if(ans=="") return "";
else
{
for(String s:strs)
{
ans=get(s,ans);
if(ans=="") return "";
}
}
return ans;
}
}
leetcode Longest Common Prefix 多个字符串的最长字串的更多相关文章
- [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 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- 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. 写一个函数找出字符串数组中 ...
- 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 ...
- 14. Longest Common Prefix (截取字符串)
Write a function to find the longest common prefix string amongst an array of strings. char* longest ...
- [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀
题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...
- LeetCode Longest Common Prefix 最长公共前缀
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
随机推荐
- highcharts时间图
这篇文章适合对highcharts已经有一定了解的猿友. 前两天想用highcharts做一个时间图,但是时间轴(x轴)的时间坐标并不是等间隔的,之前一直采用的方法是把时间做成等间隔的,然后没有数据的 ...
- Photon开发实战(2)——开发框架、第一个Photon程序
Photon基础开发框架 Photon (v4)的基本框架.开发框架主要Photon和游戏逻辑(C#)两个部分,如下图最新的Photon v4支持的4种底层协议,游戏开发逻辑Photon目前主要划分为 ...
- 网页解析Jsoup简单使用
public static void main(String[] args) throws IOException { //System.out.println("Hello World!& ...
- jQuery 标签淡入淡出 个人随笔
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 第一天的CI笔记
1 CI不区分大小写2. http://xxx.com/index/[控制器名称]/[控制器里面方法的确名称]/[传入方法的参数 ]/ 3. 控制器及控制器类名称与文件名称一致, 继承 CI_Cont ...
- asp.net取HTML控件值
asp.net取HTML控件值所有html表单里面的值控件,提交后都是以键值 key=value&key=value&……这样的形式提交给后台. radio也一样,会将选中的radio ...
- 发现一款移动web端远程调试工具weinre , 哈哈!
之前调试一直用的是chrome的远程调试,虽然效果很不错,但是在调试cordova,微信时多有不便. 在git上找工具时发现了这个:weinre,使用方法非常简单 附上git地址: https://g ...
- H5非主体结构元素
1.header元素:页面中一个内容区块或整个页面的标题: 具有引导和导航作用的结构元素,通常用来放置整个页面或页面内的一个内容区块的标题,也可以包含数据表格.搜索表单或相关的logo图片. 一个网页 ...
- Android多点触摸 与 手势识别
1. 事件类型 MotionEvent.ACTION_DOWN MotionEvent.ACTION_MOVE MotionEvent.ACTION_UP 2. 事件传递 public boolean ...
- C51应用 Modbs Rtu协议实现与KEPServerEx 通信
最近一客户要求使用STC12C5A60S2实现Modbus Rtu协议与KEPServerEx V4.0软件通信,采集单片机P2口每位的状态,设置P0口每位的状态,实现三路AD转换其中一路采集的是C0 ...