Longest Common Prefix 解答
Question
Write a function to find the longest common prefix string amongst an array of strings.
Solution
第一思路是用Trie,但是对于一道Easy类别的题目,用Trie显然是杀鸡用牛刀。
于是我们可以参考到构造Trie的过程,来找这个最长prefix。
我们先用String array中第一个String初始化Pre,然后遍历剩下的每一个String,找重合的终点,删除不重合的部分。
public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length < 1) {
return "";
}
int length = strs.length;
int initLen = strs[0].length();
StringBuilder result = new StringBuilder(initLen);
for (int i = 0; i < initLen; i++) {
result.append(strs[0].charAt(i));
}
for (int i = 1; i < length; i++) {
String curString = strs[i];
initLen = result.length();
for (int j = 0; j < initLen; j++) {
if (j < curString.length() && curString.charAt(j) == result.charAt(j)) {
continue;
}
result.delete(j, initLen);
break;
}
}
return result.toString();
}
}
更简单的方法是用String.indexOf()的方法来判断重合终点。
public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length < 1) {
return "";
}
int length = strs.length;
int initLen = strs[0].length();
String prev = strs[0];
for (int i = 0; i < length; i++) {
String curString = strs[i];
while (curString.indexOf(prev) != 0) {
prev = prev.substring(0, prev.length() - 1);
}
}
return prev;
}
}
Longest Common Prefix 解答的更多相关文章
- LeetCodeOJ刷题之14【Longest Common Prefix】
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- 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】Longest Common Prefix
题目简述: Write a function to find the longest common prefix string amongst an array of strings. 解题思路: c ...
- LintCode 78:Longest Common Prefix
public class Solution { /** * @param strs: A list of strings * @return: The longest common prefix ...
- [LintCode] Longest Common Prefix 最长共同前缀
Given k strings, find the longest common prefix (LCP). Have you met this question in a real intervie ...
- 14. Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...
- Leetcode Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...
- [LeetCode] 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. public class ...
随机推荐
- jQuery 遍历 json 方法大全
1.for循环: var obj = { "status":1, "bkmsg":"\u6210\u529f", "bkdata& ...
- HTML--鼠标事件
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- ASP.NET简单文件上传
一>使用FileUpload控件,将其拖入页面: <%@ Page Title="hehe" Language="C#" MasterPageFil ...
- PC-JS小技巧
# 事件源对象 event.srcElement.tagName event.srcElement.type # 捕获释放 event.srcElement.setCapture(); event.s ...
- Javascript:一款简易的图片切换插件
最近迷上javascript,每天不写点什么都不舒服哈~ 尽管自己能力有限,还是尽自己所能写点东西出来. 实现效果: 效果预览:http://codepen.io/anon/pen/BNjxXj 该插 ...
- Pojo和JavaBean的区别(转载)
OJO(Plain Old Java Object)这个名字用来强调它是一个普通java对象,而不是一个特殊的对象. 2005年11月时,“POJO”主要用来指代那些没用遵从特定的Java对象模型,约 ...
- 外观模式之C++实现
说明:本文仅供学习交流,转载请标明出处.欢迎转载. 在我们学习程序设计时经常会用到模块化设计的思想,这一思想是我们首先把要实现的功能用一个模块表示,当用户想完毕某个人物时依次调用相应的函数. 然而.假 ...
- 《31天成为IT服务达人》--机遇篇(二)
1 第二章 机遇就是选择大于努力 年假设你一咬牙(或者在晚点)买了房,十年的巨幅增值,比你如今干哪行都赚得快,可是往往有选择就有痛苦,这样的痛苦来至于对未知的恐惧和现实须要一定的付出.作为 ...
- python查看删除你微信的账号
#应用环境:python2.7 #!/usr/bin/env python # coding=utf-8 from __future__ import print_function import os ...
- WinForm窗体设置
属性: 软件启动后在屏幕中间 StartPosition = CenterScreen取消放大按钮 MaximizeBox = false不能拖动边框 FormBorderStyle = FixedD ...