Longest Common Prefix leetcode java
题目:
Write a function to find the longest common prefix string amongst an array of strings.
题解:
解题思路是,先对整个String数组预处理一下,求一个最小长度(最长前缀肯定不能大于最小长度)。
然后以第0个字符串作为参照,从第1个字符串到最后一个字符串,对同一位置做判断,有不同字符串返回当前记录的字符串就行。
我的代码如下,不是那么简洁好看,下面有个整理的更好一些:
- 1 private static int MinLength(String[] strs) {
- 2 int temp = Integer.MAX_VALUE;
- 3 for(int i=0; i<strs.length;i++){
- 4 if(temp>strs[i].length())
- 5 temp = strs[i].length();
- 6 }
- 7 return temp;
- 8 }
- 9
- public static String longestCommonPrefix(String[] strs) {
- if(strs.length==0){
- return "";
- }
- int j = 0;
- boolean flag = false;
- int length = MinLength(strs);
- while(j<length){
- int i = 1;
- while(i<strs.length){
- int c = strs[0].charAt(j);
- if(strs[i].charAt(j)==c){
- i++;
- }else{
- flag = true;
- break;
- }
- }
- if(flag)
- break;
- j++;
- }
- return strs[0].substring(0, j);
- }
更简洁的代码:
- 1 private int minlen(String[] strs) {
- 2 int min = Integer.MAX_VALUE;
- 3 for(int i=0; i<strs.length;i++)
- 4 min = Math.min(min,strs[i].length());
- 5 return min;
- 6 }
- 7
- 8 public String longestCommonPrefix(String[] strs) {
- 9 if (strs == null || strs.length == 0)
- return "";
- StringBuilder res = new StringBuilder();
- int index = 0;
- int len = minlen(strs);
- while (index < len){
- for (int i=1; i<strs.length;i++){
- if (strs[i].charAt(index) != strs[0].charAt(index))
- return res.toString();
- }
- res.append(strs[0].charAt(index));
- index++;
- }
- return res.toString();
- }
Reference:http://blog.csdn.net/linhuanmars/article/details/21145733
-------------------
更新
空间复杂度更小的代码如下(from discussion):
- 1 public String longestCommonPrefix(String[] strs) {
- 2 if(strs.length == 0||strs == null)
- 3 return "";
- 4
- 5 for(int i = 0; i<strs[0].length(); i++){
- 6 char x = strs[0].charAt(i);
- 7 for(int j = 1; j<strs.length; j++){
- 8 if(strs[j].length() == i || strs[j].charAt(i) != x)
- 9 return strs[0].substring(0,i);
- }
- }
- return strs[0];
- }
Longest Common Prefix leetcode java的更多相关文章
- Longest Common Prefix [LeetCode 14]
1- 问题描述 Write a function to find the longest common prefix string amongst an array of strings. 2- 思路 ...
- Longest common prefix | leetcode
Write a function to find the longest common prefix string amongst an array of strings. 思路:要去是寻找字符串ve ...
- 「Leetcode」14. Longest Common Prefix(Java)
分析 与其说是算法题,不如说是语言特性题. 这题要是对Java的String相关函数掌握的比较熟练,写起来的速度(各种意义上)就会很快. 大致的思路都是一致的,差不到哪里去,无非是枚举长度.值得一提的 ...
- 【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]题(Java): Longest Common Prefix
题目:最长公共前缀 难度:EASY 题目内容: Write a function to find the longest common prefix string amongst an array o ...
- [LeetCode][Java] Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. 题意: 写出一个函 ...
- [LeetCode] 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 ...
随机推荐
- Oceanus:美团HTTP流量定制化路由的实践
背景简述 Oceanus是美团基础架构部研发的统一HTTP服务治理框架,基于Nginx和ngx_lua扩展,主要提供服务注册与发现.动态负载均衡.可视化管理.定制化路由.安全反扒.session ID ...
- python opencv3 获取摄像头视频
git:https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 """ 捕获摄像头10 ...
- win 10 文件夹 背景 没效果
韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com
- Java基础学习——多线程之线程池
1.线程池介绍 线程池是一种线程使用模式.线程由于具有空闲(eg:等待返回值)和繁忙这种不同状态,当数量过多时其创建.销毁.调度等都会带来开销.线程池维护了多个线程,当分配可并发执行的任务时, ...
- Android之基于HTTP协议的通信详解
Android系统中本身是有下载机制的,比如浏览器使用的DownloadManager.可遗憾的是,DownloadManager只提供给浏览器使用,一般的应用程序没法调用它. 另外,如果下载调用频繁 ...
- service redis does not support chkconfig 的解决办法
问题解决办法如下: 必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中(加入以下注释): # chkconfig: # description: Redis is a persi ...
- spring---aop(10)---Spring AOP中AspectJ
写在前面 在之前的文章中有写到,Spring在配置中,会存在大量的切面配置.然而在很多情况下,SpringAOP 所提供的切面类真的不是很够用,比如想拦截制定的注解方法,我们就必须扩展DefalutP ...
- MyBatis连接SQL Server的关键点
一.Maven包配置 <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc --> < ...
- Linux线程 之 线程 线程组 进程 轻量级进程(LWP) -systemtap -mysql
http://blog.chinaunix.net/uid-24774106-id-3650136.html http://blog.itpub.net/15480802/viewspace-7627 ...
- blkblock工具1
http://www.ibm.com/developerworks/cn/linux/l-cn-perf1/ http://blog.chinaunix.net/uid-24774106-id-409 ...