Leetcode_14_Longest Common Prefix
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/40555783
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
算法是自己想的,虽然有点啰嗦,但是肯定是对的。
希望继续努力,不断提高算法的质量。每天都有所进步,加油。
算法实现代码如下:
public static String longestCommonPrefix(String[] strs) { if (strs.length == 0) return ""; String min = strs[0]; if (min.length() == 0) return ""; if (strs.length == 1) return min; for (int i = 1; i < strs.length; i++) { if (min.length() > strs[i].length()) min = strs[i]; } StringBuffer buff = new StringBuffer(); boolean flag = false; for (int i = 0; i < min.length(); i++) { char c = min.charAt(i); for (int j = 0; j < strs.length; j++) { if (strs[j].length() != 0) { if (strs[j].charAt(i) == c) { flag = true; continue; } else { flag = false; return buff.toString(); } } } if (flag) { buff.append(c); } } return buff.toString(); }
网上公认较好的解题算法如下所示:
public String longestCommonPrefix(String[] strs) { if (strs.length == 0) return ""; int size = strs.length; int j = 0; int minlength = strs[0].length(); // find the min length of strings for (String s : strs) { if (s.length() < minlength) { minlength = s.length(); } } // take substrings, put into a HashSet. if HashSet size >1, reduce the // lengh of substrings; while (j < minlength) { HashSet<String> h = new HashSet<String>(); for (int i = 0; i < size; i++) { h.add(strs[i].substring(0, minlength - j)); if (h.size() > 1) break; } if (h.size() == 1) return strs[0].substring(0, minlength - j); j++; } return ""; }
Leetcode_14_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. 解题思路: c ...
- LintCode 78:Longest Common Prefix
public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...
- [LintCode] Longest Common Prefix 最长共同前缀
Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...
- 14. Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...
- Leetcode Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- [LeetCode] 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. public class ...
- No.014:Longest Common Prefix
问题: Write a function to find the longest common prefix string amongst an array of strings. 官方难度: Eas ...
- [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀
题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...
随机推荐
- mybatis自动生成
很简单只要配两个文件 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmln ...
- 函数的形参和实参之arguments对象
当函数调用函数时候传入的实参比函数声明时候制定的形参要少时候,剩余的形参就设置成了undefined.例如 function getPropertyNames(o,/*optional*/a){ va ...
- ubuntu14.04+sublime3+latex配置
目的:用题目所说的三个东西写论文. 配置方法:参考 http://blog.csdn.net/bleedingfight/article/details/72810606, 但该博客所提的texliv ...
- 63. Unique Paths II(中等, 能独立做出来的DP类第二个题^^)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- reload(sys)后print失效问题解决
python版本: python2.7.6 #查看python默认编码格式 >>> import sys >>> print sys.getdefaultencod ...
- JS判断PC还是移动端打开网页
最近在做移动端网站,也需兼容PC端.还没找到更好的方法,只能用javascr判断用户是在PC端打开还是移动端打开. JS判断 var isPC = function (){ var userAg ...
- docker环境 快速使用elasticsearch-head插件
docker环境 快速使用elasticsearch-head插件 #elasticsearch配置 #进入elk容器 docker exec -it elk /bin/bash #head插件访问配 ...
- Request JSON
https://developer.android.com/training/volley/request.html Request JSON Volley provides the followin ...
- 全面剖析Redis Cluster原理和应用
全面剖析Redis Cluster原理和应用 1.Redis Cluster总览 1.1 设计原则和初衷 在官方文档Cluster Spec中,作者详细介绍了Redis集群为什么要设计成现在的样子.最 ...
- Ruby 连接MySQL数据库
使用Ruby连接数据库的过程还真的是坎坷,于是写点文字记录一下. 简介 Ruby简介 RubyGems简介 包管理之道 比较著名的包管理举例 细说gem 常用的命令 准备 驱动下载 dbi mysql ...