本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  2. 【leetcode】Longest Common Prefix

    题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...

  3. LintCode 78:Longest Common Prefix

      public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...

  4. [LintCode] Longest Common Prefix 最长共同前缀

    Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...

  5. 14. Longest Common Prefix

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

  6. Leetcode Longest Common Prefix

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

  7. [LeetCode] 14. Longest Common Prefix

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

  8. No.014:Longest Common Prefix

    问题: Write a function to find the longest common prefix string amongst an array of strings. 官方难度: Eas ...

  9. [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀

    题记: 这道题不难但是很有意思,有两种解题思路,可以说一种是横向扫描,一种是纵向扫描. 横向扫描:遍历所有字符串,每次跟当前得出的最长公共前缀串进行对比,不断修正,最后得出最长公共前缀串. 纵向扫描: ...

随机推荐

  1. idea热部署

    <!-- 热部署模块 --> <dependency> <groupId>org.springframework.boot</groupId> < ...

  2. select动态绑定vue.js

    <select v-model="selected"> <option v-for="option in options" v-bind:va ...

  3. java客户端Jedis操作Redis Sentinel 连接池

    pom配置: <dependency> <groupId>org.springframework.data</groupId> <artifactId> ...

  4. brew的MAC安装

    1.介绍 brew是一个软件包管理工具,类似于centos下的yum或者ubuntu下的apt-get,非常方便,免去了自己手动编译安装的不便 brew 安装目录 /usr/local/Cellar ...

  5. 使用CSS让多出来的字变为省略号

    <style> .text1 { width:200px; overflow:hidden; text-overflow:ellipsis; -o-text-overflow:ellips ...

  6. AJAX 向后台发送带 List 集合的对象

    现有基类: public class School { int name; int address; List<Student> students = new ArrayList<S ...

  7. linkList hashSet ArrayList IO 序列化 1.1.瞬态transient .字符编码表 Properties

      Day12 IO  序列化 .递归_递归的概念_注意事项 1.递归:方法的递归调用--它是一种方法调用的方式--方法可以调用其本身 2.注意事项: 1).递归必须要有一个"出口(结束的条 ...

  8. centos 挂载ntfs格式的移动硬盘

    经查找资料发现,linux也是可以支持ntfs格式分区的,只是需要安装ntfs-3g插件. CentOS挂载ntfs移动硬盘的具体步骤: 1 安装fuse. 下载fuse-2.9.3.tar.gz   ...

  9. JS中的DOM— —节点以及操作

    DOM操作在JS中可以说是非常常见了吧,很多网页的小功能的实现,比如一些元素的增删操作等都可以用JS来实现.那么在DOM中我们需要知道些什么才能完成一些功能的实现呢?今天这篇文章就先简单的带大家入一下 ...

  10. Java第6次实验提纲(异常)

    PTA与参考资料 题集:集合 异常实验文件 第1次实验 1.1 7-1 常用异常 如何进行强制转换 如何捕获多种类型的异常 1.2 7-2 使用异常机制处理异常输入 在哪里加catch 1.3 7-3 ...