[LeetCode]-011-Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
[]
=>""
["abcweed","htgdabc","sabcrf"]
=>""
["abcweed","abhtgdc","abacrf"]
=>"ab"
题目大意:求字符串数组的最长子前缀
- public class Solution{
- public String longestCommonPrefix(String[] strs) {
- //System.out.println(Arrays.toString(strs));
- if(strs.length == 0)
- return "";
- int min = Integer.MAX_VALUE;
- for(String s : strs){
- if(min>s.length())
- min = s.length();
- }
- if(min==0)
- return "";
- //System.out.println(min);
- String prefix = "", tmp = "";
- int i=0;
- for( ; i<min; i++){
- prefix = strs[0].substring(0,(i+1));
- //System.out.println("prefix=" + prefix);
- for(int j=1; j<strs.length; j++){
- tmp = strs[j].substring(0,(i+1));
- //System.out.println("tmp=" + tmp);
- if(!prefix.equals(tmp))
- return strs[0].substring(0,(i));
- }
- }
- System.out.println("i=" + strs[0].substring(0,(i)));
- return "";
- }
- public static void main(String[] args){
- String[] arr = {"","asdffg","aswd"};
- if(args.length!=0)
- arr = args;
- Solution solution = new Solution();
- String res = solution.longestCommonPrefix(arr);
- System.out.println(res);
- }
- }
[LeetCode]-011-Longest Common Prefix的更多相关文章
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- [LeetCode] 14. Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- 【leetcode】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- [LeetCode] 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. public class ...
- 【JAVA、C++】LeetCode 014 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 解题思路: 老实遍历即可, ...
- 【leetcode】Longest Common Prefix (easy)
Write a function to find the longest common prefix string amongst an array of strings. 思路:找最长公共前缀 常规 ...
- Java [leetcode 14] Longest Common Prefix
小二好久没有更新博客了,真是罪过,最近在看linux的东西导致进度耽搁了,所以今晚睡觉前怒刷一题! 问题描述: Write a function to find the longest common ...
- Leetcode 14——Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...
- LeetCode(54)-Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路: 题意:找出 ...
- [leetcode]14. Longest Common Prefix 最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
随机推荐
- springboot2.0结合fastdfs实现文件分布式上传
1. 引入依赖 在父工程中,我们已经管理了依赖,版本为: <fastDFS.client.version>1.26.7</fastDFS.client.version> 因此, ...
- Python 入门之 内置模块 -- random模块
Python 入门之 内置模块 -- random模块 1.random模块 import random # random -- 随机数 (1)选择1-50之间随机的整数 print(random.r ...
- php常用header状态
<?php //200 正常状态 header('HTTP/1.1 200 OK'); // 301 永久重定向,记得在后面要加重定向地址 Location:$url header('HTTP/ ...
- CentOS7下载与安装错误全记录
这篇文章记录安装CentOS7过程错误全记录,供大家和自己参考 起因:笔记本用的win10系统,开启热点的时候,总是10分钟就自动关闭.于是折腾linux系统,平时用win10系统,也切换到linux ...
- log4net日志输出配置即输出到文件又输出到visual studio的output窗口
<configuration> <configSections> <section name="log4net" type="log4net ...
- ubuntu编译安装swoole (存多版本php时)
一 切换php版本 见 https://www.cnblogs.com/bushuwei/p/11699503.html 二 编译安装swoole 这里对pecl安装不做介绍,以下是编译安装,复制 ...
- 实现一个名为Person的类和它的子类Employee,Manager是Employee的子类,设计一个方法add用于涨工资,普通员工一次能涨10%,经理能涨20%。
1.实现一个名为Person的类和它的子类Employee,Manager是Employee的子类,设计一个方法add用于涨工资,普通员工一次能涨10%,经理能涨20%.具体要求如下:(1)Perso ...
- elementui 之el-pagination爬坑,属性pager-count的设定
我想设置总页数为10页,页码条总是显示两个页码,其余省略号显示,我选择了pager-count,如下: <el-pagination @size-change="handleSizeC ...
- 2019-11-29-VisualStudio-2019-尝试使用-C#-8.0-新的方式
title author date CreateTime categories VisualStudio 2019 尝试使用 C# 8.0 新的方式 lindexi 2019-11-29 08:41: ...
- python-ssh-远程服务器+远程docker执行命令
在python语言中实现远程服务器执行命令+远程dcoker执行命令 def ssh_exec_command(ip, username, password, cmd=None): "&qu ...