[LeetCode][Java] Longest Common Prefix
题目:
Write a function to find the longest common prefix string amongst an array of strings.
题意:
写出一个函数。找到一组数组中的最长公共子串。
算法分析:
须要构建两重循环。第一层是最短子串的长度,还有一层是遍历字符串数组中的每一个成员。
brute force的想法,以第一个字符串为标准。对于每一个字符串从第一个字符開始,看看是不是和标准一致,假设不同,则跳出循环返回当前结果。否则继续下一个字符。
AC代码:
public String longestCommonPrefix(String[] strs)
{
if(strs == null || strs.length == 0)
return ""; int minLen=Integer.MAX_VALUE;
for(String str: strs)
{
if(minLen > str.length())
minLen = str.length();//找到这些字符串中的最短子串
}
if(minLen == 0) return ""; for(int j=0; j<minLen; j++)
{
char prev='0';
for(int i=0; i<strs.length ;i++)
{
if(i==0)
{
prev = strs[i].charAt(j);//每次仅仅取第一个字符串的头一个字符測试
continue;
} if(strs[i].charAt(j) != prev)//若全部的字符串的这个字符都相等,就取下一个字符,若不相等,就返回这个字符前面的那个些公共前缀
{
return strs[i].substring(0, j);
}
}
} return strs[0].substring(0,minLen);//全部字符串在最短字符串长度下前缀都相等
}
[LeetCode][Java] Longest Common Prefix的更多相关文章
- 【JAVA、C++】LeetCode 014 Longest Common Prefix
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. If there is n ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
- 【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 ...
- 【leetcode】Longest Common Prefix (easy)
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. 很简单的一个描述,最 ...
- LeetCode(54)-Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路: 题意:找出 ...
随机推荐
- (转)iOS开发之同一应用设置不同图标和名称
本文转自:http://www.devzeng.com/blog/ios-two-version-app-setting-profile.html iOS开发之同一应用设置不同图标和名称 SEP 6T ...
- LeetCode(96) Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...
- CodeForces 149D 区间DP Coloring Brackets
染色有三个条件: 对于每个点来说要么不染色,要么染红色,要么染蓝色 对于每对配对的括号来说,有且只有一个一边的括号被染色 相邻的括号不能染成相同的颜色 首先可以根据给出的括号序列计算出括号的配对情况, ...
- 03007_HttpServlet
1.创建 new---Servlet package com.gzdlh.servlet; import java.io.IOException; import javax.servlet.Servl ...
- mac osx下apache下的坑: you don’t have permission to access / on this server
在Mac下Apache修改默认站点的目录时,遇到403错误, you don’t have permission to access / on this server 首先按照google到教程: 修 ...
- 九度oj 题目1472:求两个多项式的和
题目描述: 输入两个多项式,计算它们的和. 每个多项式有若干对整数表示,每组整数中,第一个整数表示系数(非0),第二个整数表示该项的次数. 如由3 3 5 -2 1 4 0表示3x^5 - 2 * x ...
- 集群高可用之lvs+keepalive
集群高可用之lvs+keepalive keepalive简介: 负载均衡架构依赖于知名的IPVS内核模块,keepalive由一组检查器根据服务器的健康情况动态维护和管理服务器池.keepalive ...
- HackerRank# Candies
原题地址 LeetCode上也有这道题,直接扫一遍就行了,连数组都不用开,感觉像是蕴含了某种动归的思想在里面,要不怎么是个动归题呢 代码: #include <cmath> #includ ...
- 刷题总结——射箭(bzoj2732)
题目: Description 沫沫最近在玩一个二维的射箭游戏,如下图 1 所示,这个游戏中的 x 轴在地面,第一象限中有一些竖直线段作为靶子,任意两个靶子都没有公共部分,也不会接触坐标轴.沫沫控制一 ...
- Split The Tree
Split The Tree 时间限制: 1 Sec 内存限制: 128 MB 题目描述 You are given a tree with n vertices, numbered from 1 ...