LeetCode_OJ【14】Longest Common Prefix
来源:互联网 发布:java super不需要 编辑:程序博客网 时间:2024/06/05 16:13
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) return null;if(strs.length == 0)return "";String prefix = strs[0];for(int i = 1 ; i < strs.length ; i++){ if(strs[i].length() == 0)return "";for(int j = 0 ; j < strs[i].length() ; j++){if(prefix.length() < j+1)break;if(prefix.charAt(j) != strs[i].charAt(j))prefix = prefix.substring(0,j);if(j == strs[i].length()-1 && prefix.length() > strs[i].length())prefix = prefix.substring(0,j+1);}} return prefix; }}
0 0
- LeetCode_OJ【14】Longest Common Prefix
- 14、Longest Common Prefix
- 14,Longest Common Prefix
- #14 Longest Common Prefix
- 14Longest Common Prefix
- 14Longest Common Prefix
- 14 Longest Common Prefix
- 【14】Longest Common Prefix
- 14-Longest Common Prefix
- LeetCode 14: Longest Common Prefix
- LeetCode(14) Longest Common Prefix
- LeetCode 14 Longest Common Prefix
- [leetcode 14] Longest Common Prefix
- leetcode 14-Longest Common Prefix
- [leetcode 14] Longest Common Prefix
- [Leetcode] 14 - Longest Common Prefix
- LeetCode | #14 Longest Common Prefix
- leetcode.14----------Longest Common Prefix
- 两个div之间有间隔
- ios状态栏颜色设置
- Invoke
- Python编码问题
- hdu acm 2602 Bone Collector
- LeetCode_OJ【14】Longest Common Prefix
- 制作根文件系统:make[1]: *** [miscutils/ubi_tools.o] 错误 1 --来源于网络
- UIScrollView/UIPageControl/
- 基于 Node-red实现opencv边缘检测节点
- Eclipse安装Maven插件Maven Integration for Eclipse
- 软件开发项目管理心得(一)
- 整理了一份React-Native学习指南
- Codeforces #319(Div.2) A. Multiplication Table
- 常用机器学习方法总结