GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/code/lc14.java at master · mJackie/leetcode Expand file tree/ Copy pathlc14.javaMore file actions 28 lines (27 loc) · 784 Bytes / Copy pathlc14.javaFile metadata and controls28 lines (27 loc) · 784 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package code; /* * 14. Longest Common Prefix * 题意:找最长匹配前缀 * 难度:Easy * 分类:String * Tips:可先将字符串排序,之后只比较数组中首和尾字符串即可 */ import java.util.Arrays; public class lc14 { public static String longestCommonPrefix(String[] strs) { //不是最优的方法,多做了比较 if(strs.length==0) return ""; Arrays.sort(strs); char[] str1 = strs[0].toCharArray(); char[] str2 = strs[strs.length-1].toCharArray(); int len = Math.min(str1.length,str2.length); int i = 0; while(i<len){ if(str1[i] == str2[i]) i++; else break; } return strs[0].substring(0,i); } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |