Write a function to find the longest common prefix string amongst an array of strings.


题解:以strs[0]为模板,每次挨个查看是否所有的串里面是否第i位上都和strs[0]一样,如果都一样,把i位置上的字符放到answer里面,i++,继续循环,否则返回当前的answer。

代码如下:

 public class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0)
return ""; StringBuffer answer = new StringBuffer();
for(int i = 0;i < strs[0].length();i++){
char now = strs[0].charAt(i);
for(int j = 1;j < strs.length;j++){
if(i>strs[j].length()-1 || strs[j].charAt(i) != now)
return answer.toString();
}
answer.append(now);
}
return answer.toString();
}
}

【leetcode刷题笔记】Longest Common Prefix的更多相关文章

  1. leetCode练题——14. Longest Common Prefix

    1.题目 14. Longest Common Prefix   Write a function to find the longest common prefix string amongst a ...

  2. LeetCode专题-Python实现之第14题:Longest Common Prefix

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  3. leetcode【14题】Longest Common Prefix

    题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...

  4. 【LeetCode每天一题】Longest Common Prefix(最长前缀)

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  5. 【LeetCode】LeetCode——第14题:Longest Common Prefix

    14. Longest Common Prefix My Submissions Question Editorial Solution Total Accepted: 97052 Total Sub ...

  6. 【LeetCode OJ 14】Longest Common Prefix

    题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...

  7. Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  8. 【LeetCode算法-14】Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  9. LeetCode记录之14——Longest Common Prefix

    本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest ...

随机推荐

  1. Linux终端:speedtest_cli检测你的实时带宽速度

    你在家(或者办公室)的上传和下载速度如何?你能保证,你支付费用给ISP的同时得到了等价的回报? 要想测试我们因特网连接的速度,当下存在着一些因特网服务,比如说SpeedTest,这是一种可以通过Web ...

  2. git个人使用总结(界面版)

    最近开始使用GIT来管理测试文档,从0到1开始使用git 1.首先,使用网页登录GIT后,创建项目 2.创建项目后,需要配置一下访问者权限 3.然后在网页版GIT复制地址,git clone到 本地 ...

  3. NSNotification的几点说明

    1.NSNotification消息的同步性 ①NSNotification使用的是同步操作.即如果你在程序中的A位置post了一个NSNotification,在B位置注册了一个observer,通 ...

  4. C#常见的概念阐述

    在上篇文章中,你跟着我写了一个HelloWorld,本篇中,我们来谈谈一些C#程序中的小概念 1.C# 程序结构 一个 C# 程序主要包括以下部分: 命名空间声明(Namespace declarat ...

  5. JQuery find函数选择器使用

  6. Spring Boot(六): Favicon配置

    1.默认的Favicon Spring Boot提供了一个默认的Favicon,每次访问应用的时候都能看到,如图所示. 2.关闭Favicon 我们可以在application.properties中 ...

  7. spark-submit 提交任务

    将工程打成jar 放入到linux中 切换到[root@node4 Desktop]# cd /usr/local/development/spark-2.0-hadoop2.6/bin/ 输入命令 ...

  8. emacs的常用配置备份

    据说有人搞丢了自己的emacs的配置,然后一怒之下抛弃了emacs投身vim,我还是做个emacs配置的备份吧, 虽然我现在也算不上emacs的发烧友. 这里的配置大多是从网上参考的,最多的是下面的链 ...

  9. PHP 使用 GeoLiteCity 库解析 IP 为地理位置

    关于把 IP 地址转换为地理位置可以使用网络上很多的 API,好处就是不用在本地存储一个 IP 数据库,而且一般网络上的 IP 库会自动更新,不利的地方就是太依赖于网络,性能表现也可能会弱些.比如像下 ...

  10. WPF使用X:Static做多语言支持

    让程序支持多语言,一般把需要显示的字符串保存在一个资源类的static属性中. <!--[if !supportLists]--> <!--[endif]--> 微软的WPF程 ...