[LeetCode] 14. Longest Common Prefix ☆
Write a function to find the longest common prefix string amongst an array of strings.
解法:
广度优先搜索:先比较所有字符串的第一个字符,然后比较第二个。。。。如果某一行没有了(说明其为最短的单词)或者遇到不匹配字符,则返回当前匹配到的最长前缀。
public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
} for (int i = 0; i < strs[0].length(); i++) {
for (int j = 1; j < strs.length; j++) {
if (strs[j].length() <= i || strs[j].charAt(i) != strs[0].charAt(i)) {
return strs[0].substring(0, i);
}
}
}
return strs[0];
}
}
深度优先搜索:依次将找出的最长前缀res(初始为第一个字符串)与后面的字符串匹配,如果共同前缀变短,更新res。
public class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
} String res = strs[0];
for (int i = 1; i < strs.length; i++) {
for (int j = 0; j < res.length(); j++) {
if (strs[i].length() <= j || res.charAt(j) != strs[i].charAt(j)) {
res = strs[i].substring(0, j);
break;
}
}
}
return res;
}
}
[LeetCode] 14. Longest Common Prefix ☆的更多相关文章
- Leetcode 14. Longest Common Prefix(水)
14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...
- [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] 14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. public class ...
- 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]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最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- LeetCode——14. Longest Common Prefix
一.题目链接:https://leetcode.com/problems/longest-common-prefix/ 二.题目大意: 给定若干个字符串,找出它们的最长公共子串. 三.题解: 这道题目 ...
随机推荐
- [leetcode-753-Open the Lock]
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', ...
- Python高级编程-使用SQLite
SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在iOS和Android的App中都可以集成. Python就 ...
- 最长回文子串计算(fail)
题意: 给定一个字符串s,在s中找到最长的回文子字符串.您可以假设s的最大长度为1000. 例子: 输入: “babad” 输出: “bab” 注: “aba”也是一个有效的答案. 我的答案: 想法: ...
- 原生javascript自定义input[type=checkbox]效果
2018年6月27日 更新 能用css3,就不用js 用纯css3实现样式重写 <!DOCTYPE html> <html lang="en"> < ...
- django使用ajax提交表单数据报403错解决方法
只需要在.ajaxSetup方法中设置csrfmiddlewaretoken即可 $.ajaxSetup({ data: {csrfmiddlewaretoken: '{{ csrf_token }} ...
- VMbox复制虚拟机后网卡问题-bring up interface eth0:Device eth0 does not seem to be present
1.使用 ifconfig -a 查看mac地址 eg:HWaddr:08:00:29:B2:2B 2.vi /etc/sysconfig/network-scripts/ifcfg-eth0 将 ...
- Ninject学习资料
https://github.com/ninject/Ninject/wiki/Modules-and-the-Kernel http://www.cnblogs.com/willick/p/3223 ...
- 关于设置Visaul Studio 2010 代码编辑界面背景的方法
1.打开代码编辑界面: 2.找到工具--选项: 3.打开选项后选中纯文本--项背景色: 4.点击自定义,找到自己需要的颜色: [注]: “项前景色”即代码的颜色: “项背景色”即背景颜色. 设置好后, ...
- memcached安装与启动
windows 安装1.4.4版本 https://pan.baidu.com/s/1xX1NThLqeq2zNMaqONFgkQ 解压,“以管理员身份” 运行cmd,切换到memcached根目录, ...
- 青花瓷运用->下载历史版本App
1.软件准备 [必备]Charles4.0.1 下载密码: jfnk [不需要,配合Charles食用效果更佳]Paw2.3.1 下载密码: t3my 2.正式开始 2.1 打开Charles青花瓷 ...