本文是在学习中的总结,欢迎转载但请注明出处: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. oclazyload的尝试

    https://oclazyload.readme.io/docs http://www.cnblogs.com/BestMePeng/p/AngularJS_ocLazyLoad.html 模块依赖 ...

  2. JavaScript Window History

    window.history 对象包含浏览器的历史. Window History window.history对象在编写时可不使用 window 这个前缀. 为了保护用户隐私,对 JavaScrip ...

  3. Python3 OS 文件/目录方法

    os 模块提供了非常丰富的方法用来处理文件和目录.常用的方法如下表所示: 序号 方法及描述 1 os.access(path, mode) 检验权限模式 2 os.chdir(path) 改变当前工作 ...

  4. 使用Docker搭建GitLab

    使用docker-compose快速启动GitLab.(当然前提是你先安装docker-compose,安装方式见博客:http://blog.csdn.net/yulei_qq/article/de ...

  5. GDAL打开mdb文件失败解决方法

    使用GDAL打开mdb文件时提示下面错误信息: ERROR 1: Unable to initialize ODBC connection to DSN for DRIVER=Microsoft Ac ...

  6. Gradle 1.12用户指南翻译——第四十七章. Build Init 插件

    本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  7. postgresql 登录查看表定义

    su - postgres psql \connect database_name; \d table_name

  8. 数据库查询优化——Mysql索引

    工作一年了,也是第一次使用Mysql的索引.添加了索引之后的速度的提升,让我惊叹不已.隔壁的老员工看到我的大惊小怪,平淡地回了一句"那肯定啊". 对于任何DBMS,索引都是进行优化 ...

  9. JDK 源码学习——ByteBuffer

    ByteBuffer 在NIO的作用 Java SE4 开始引入Java NIO,相比较于老的IO,更加依赖底层实现.引入通道(Channels),选择器(selector),缓冲(Buffers). ...

  10. 剑指Offer——知识点储备-数据库基础

    剑指Offer--知识点储备-数据库基础 数据库 事务 事务的四个特性(ACID):   原子性(Atomicity).一致性(Consistency).隔离性(Isolation).持久性(Dura ...