File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public String longestCommonPrefix (String [] strs ) {
3+ int n = strs .length ;
4+ if (n == 0 ) return "" ;
5+ String prefix = strs [0 ];
6+ for (int i =1 ; i <n ; i ++){
7+ int j = 0 ;
8+ while (j <prefix .length () && j <strs [i ].length () && prefix .charAt (j ) == strs [i ].charAt (j )){
9+ j ++;
10+ }
11+ prefix = prefix .substring (0 , j );
12+ if (prefix .length () == 0 ){
13+ return "" ;
14+ }
15+ }
16+ return prefix ;
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public String longestCommonPrefix (String [] strs ) {
3+ int n = strs .length ;
4+ if (n == 0 ) return "" ;
5+ String prefix = strs [0 ];
6+ for (int i =1 ; i <n ; i ++){
7+ int j = 0 ;
8+ while (j <prefix .length () && j <strs [i ].length () && prefix .charAt (j ) == strs [i ].charAt (j )){
9+ j ++;
10+ }
11+ prefix = prefix .substring (0 , j );
12+ if (prefix .length () == 0 ){
13+ return "" ;
14+ }
15+ }
16+ return prefix ;
17+ }
18+ }
You can’t perform that action at this time.
0 commit comments