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. Windows下创建.gitgnore文件

    相信使用过git的朋友可能遇到过,直接在windows下创建.gitgnore文件失败.类似截图那样 上网查了一下,有两种方法. 方法1: 此方法较为简单,前提是安装了git bash. 用git b ...

  2. 我们常用的在a标签中有点击事件

    我们常用的在a标签中有点击事件:1. a href="javascript:js_method();" 这是我们平台上常用的方法,但是这种方法在传递this等参数的时候很容易出问题 ...

  3. 01-2制作U盘启动盘--装机助理工具

    在可以上网的电脑上执行制作启动盘的操作: 打开浏览器输入:http://www.zhuangjizhuli.com/upan.html 1.制作U盘启动盘: 2.下载 装机助理 软件 3.步骤: 第一 ...

  4. JSON Web Token (JWT) 实现与使用方法

    1. JSON Web Token是什么 JSON Web Token (JWT)是一个开放标准(RFC 7519),它定义了一种紧凑的.自包含的方式,用于作为JSON对象在各方之间安全地传输信息.该 ...

  5. winform对话框拖拽显示文件路径的问题

    allow drop=true; dragEnter dragDrop vs管理员账户拖拽会失败

  6. Struts2学习之拦截器栈

    © 版权声明:本文为博主原创文章,转载请注明出处 拦截器栈: - 从结构上看:拦截器栈相当于多个拦截器的组合 - 从功能上看:拦截器栈也是拦截器 默认拦截器栈: - 在struts-core.jar中 ...

  7. JFinal中json的使用

    之前Java开发一直使用的是经典的ssh,去年接触了jfinal,觉得jfinal的魅力非常之大,让我无法自拔,现在还深深地陷在其中. 简单的介绍一下jfinal,jfinal短小精悍,让java有了 ...

  8. java多线程编码注意事项

    Sole purpose of using concurrency is to produce scalable and faster program. But always remember, sp ...

  9. Springboot Maven 多模块项目中 @Service跨模块引用失败的问题

    子模块中引用另一个子模块中的Service, @Autowired失败. 添加了模块之间的依赖没解决. 组以后在启动类上加上 @SpringBootApplication(scanBasePackag ...

  10. jfreechart折线图 demo

    public class ChartUtil { public static ChartUtil chartUtil; private RoomViewsDataService roomViewsDa ...