Longest Common Prefix
来源:互联网 发布:linux监控软件对比 编辑:程序博客网 时间:2024/04/28 17:07
Write a function to find the longest common prefix string amongst an array of strings.
Analysis: First compute the common prefix of the first two strings and then use the result compare with the rest of the strings in that array.
public class Solution { public String longestCommonPrefix(String[] strs) { String common = ""; if(strs.length==0) return common; if(strs.length==1) return strs[0]; // got common prefix from the first two strings String s0 = strs[0]; String s1 = strs[1]; for(int i=0; i<(s0.length()<=s1.length()?s0.length():s1.length()); i++) { if(s0.charAt(i)==s1.charAt(i)) common+=s0.charAt(i); else break; } if(common=="") return common; // iterate the rest strings in the array to got the longest common prefix for(int j=2; j<=strs.length-1; j++) { String temp = ""; for(int k=0; k<(common.length()<=strs[j].length()?common.length():strs[j].length()); k++) { if(common.charAt(k) == strs[j].charAt(k)) { temp += strs[j].charAt(k); } else { if(k==0) return ""; else break; } } common = temp; } return common; }}
0 0
- LeetCode: Longest Common Prefix
- LeetCode Longest Common Prefix
- LeetCode : Longest Common Prefix
- [Leetcode] Longest Common Prefix
- leetcode6:Longest Common Prefix
- leetcode22: longest common prefix
- [LeetCode] Longest Common Prefix
- Leetcode14: Longest Common Prefix
- Longest Common Prefix
- Longest Common Prefix
- Longest Common Prefix
- [LeetCode]Longest Common Prefix
- Longest Common Prefix
- LeetCode-Longest Common Prefix
- Longest Common Prefix
- [LeetCode] Longest Common Prefix
- Longest Common Prefix
- LeetCode - Longest Common Prefix
- 使用pidstat查看进程资源使用情况
- JQUERY获取当前页面的URL信息
- Python模块学习之字符串方法——Python学习手册笔记
- C# WinForm多线程(一)----- Thread类库
- png压缩软件
- Longest Common Prefix
- c#中多线程访问winform控件的若干问题
- 弟弟的作业
- oracle gg 简单使用
- 正则表达式30分钟入门教程
- Spring Web Flow 2.0 入门详解
- C#的WinForm多线程应用(转)
- How to Interpret the OS stats section of an AWR report (文档 ID 762526.1)
- ThreadLocal详解