GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/code/lc14.java at master · mJackie/leetcode

Expand file tree

/

lc14.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
28 lines (27 loc) · 784 Bytes
/

lc14.java

Copy path

File metadata and controls

28 lines (27 loc) · 784 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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.