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. Mysql-Proxy实现mysql读写分离、负载均衡 (转)

    在mysql中实现读写分离.负载均衡,用Mysql-Proxy是很容易的事,不过大型处理对于性能方面还有待提高,主要配置步骤如下: 1.1. mysql-proxy安装 MySQL Proxy就是这么 ...

  2. Linux基础ls命令

    ls 命令是linux下最常用的命令,通过ls 命令不仅可以查看linux文件夹包含的文件而且可以查看文件权限(包括目录.文件夹.文件权限)查看目录信息等等.ls 命令在日常的linux操作中用的 ...

  3. CGI是什么 搜索了这么多,大致看明白了保留下来。

    转自:http://blog.chinaunix.net/uid-13408389-id-2894933.html 分类: CGI是什么 CGI是CommonGatewayInterface 的简称. ...

  4. html自定义标签属性

    <a href="#" _asd="xxxx" onclick="test(event)">test</a> < ...

  5. string 和 stringbuffer的区别?

    string和stringbuffer的区别其实是变量和常亮的关系,string和stringbuffer内部实现的原理不同,在修改string对象时会产生另外的对象,也就是说在内存中会有两个存储区域 ...

  6. 详解path和classpath的区别

    详解path和classpath的区别 path的作用 path是系统用来指定可执行文件的完整路径,即使不在path中设置JDK的路径也可执行JAVA文件,但必须把完整的路径写出来,如C:\Progr ...

  7. ubuntu 1204 安装mysql

    检測本机是否有安装mysql sudo netstat -tap | grep mysql 运行上面的命令之后.看到 mysq 的socket 处于监听状态,说明有成功安装. 安装mysql sudo ...

  8. 登录shell与非登录shell读取文件过程

    登录shell与非登录shell读取文件过程登录:/etc/profile→/etc/profile.d/*.sh        ~/.bash_profile非登录:~/.bash_profile→ ...

  9. POJ 1860

    须要推断是否有正权环存在,Bellman-Ford算法就能够辣~ AC代码: #include <iostream> #include <cstdio> #include &l ...

  10. Collect More Jewels(hdu1044)(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...