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

[]
=>""
["abcweed","htgdabc","sabcrf"]
=>""
["abcweed","abhtgdc","abacrf"]
=>"ab"

题目大意:求字符串数组的最长子前缀

  1. public class Solution{
  2. public String longestCommonPrefix(String[] strs) {
  3. //System.out.println(Arrays.toString(strs));
  4. if(strs.length == 0)
  5. return "";
  6. int min = Integer.MAX_VALUE;
  7. for(String s : strs){
  8. if(min>s.length())
  9. min = s.length();
  10. }
  11. if(min==0)
  12. return "";
  13. //System.out.println(min);
  14. String prefix = "", tmp = "";
  15. int i=0;
  16. for( ; i<min; i++){
  17. prefix = strs[0].substring(0,(i+1));
  18. //System.out.println("prefix=" + prefix);
  19. for(int j=1; j<strs.length; j++){
  20. tmp = strs[j].substring(0,(i+1));
  21. //System.out.println("tmp=" + tmp);
  22. if(!prefix.equals(tmp))
  23. return strs[0].substring(0,(i));
  24. }
  25. }
  26. System.out.println("i=" + strs[0].substring(0,(i)));
  27. return "";
  28. }
  29.  
  30. public static void main(String[] args){
  31. String[] arr = {"","asdffg","aswd"};
  32. if(args.length!=0)
  33. arr = args;
  34. Solution solution = new Solution();
  35. String res = solution.longestCommonPrefix(arr);
  36. System.out.println(res);
  37. }
  38. }

[LeetCode]-011-Longest Common Prefix的更多相关文章

  1. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

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

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  3. 【leetcode】Longest Common Prefix

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

  4. [LeetCode] 14. Longest Common Prefix

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

  5. 【JAVA、C++】LeetCode 014 Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...

  6. 【leetcode】Longest Common Prefix (easy)

    Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...

  7. Java [leetcode 14] Longest Common Prefix

    小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...

  8. Leetcode 14——Longest Common Prefix

    题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...

  9. LeetCode(54)-Longest Common Prefix

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路: 题意:找出 ...

  10. [leetcode]14. Longest Common Prefix 最长公共前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

随机推荐

  1. springboot2.0结合fastdfs实现文件分布式上传

    1. 引入依赖 在父工程中,我们已经管理了依赖,版本为: <fastDFS.client.version>1.26.7</fastDFS.client.version> 因此, ...

  2. Python 入门之 内置模块 -- random模块

    Python 入门之 内置模块 -- random模块 1.random模块 import random # random -- 随机数 (1)选择1-50之间随机的整数 print(random.r ...

  3. php常用header状态

    <?php //200 正常状态 header('HTTP/1.1 200 OK'); // 301 永久重定向,记得在后面要加重定向地址 Location:$url header('HTTP/ ...

  4. CentOS7下载与安装错误全记录

    这篇文章记录安装CentOS7过程错误全记录,供大家和自己参考 起因:笔记本用的win10系统,开启热点的时候,总是10分钟就自动关闭.于是折腾linux系统,平时用win10系统,也切换到linux ...

  5. log4net日志输出配置即输出到文件又输出到visual studio的output窗口

    <configuration> <configSections> <section name="log4net" type="log4net ...

  6. ubuntu编译安装swoole (存多版本php时)

    一  切换php版本 见 https://www.cnblogs.com/bushuwei/p/11699503.html 二  编译安装swoole 这里对pecl安装不做介绍,以下是编译安装,复制 ...

  7. 实现一个名为Person的类和它的子类Employee,Manager是Employee的子类,设计一个方法add用于涨工资,普通员工一次能涨10%,经理能涨20%。

    1.实现一个名为Person的类和它的子类Employee,Manager是Employee的子类,设计一个方法add用于涨工资,普通员工一次能涨10%,经理能涨20%.具体要求如下:(1)Perso ...

  8. elementui 之el-pagination爬坑,属性pager-count的设定

    我想设置总页数为10页,页码条总是显示两个页码,其余省略号显示,我选择了pager-count,如下: <el-pagination @size-change="handleSizeC ...

  9. 2019-11-29-VisualStudio-2019-尝试使用-C#-8.0-新的方式

    title author date CreateTime categories VisualStudio 2019 尝试使用 C# 8.0 新的方式 lindexi 2019-11-29 08:41: ...

  10. python-ssh-远程服务器+远程docker执行命令

    在python语言中实现远程服务器执行命令+远程dcoker执行命令 def ssh_exec_command(ip, username, password, cmd=None): "&qu ...