File tree Expand file tree Collapse file tree 5 files changed +111
-85
lines changed
solution/0000-0099/0014.Longest Common Prefix Expand file tree Collapse file tree 5 files changed +111
-85
lines changed Original file line number Diff line number Diff line change 3737<!-- 这里可写当前语言的特殊实现逻辑 -->
3838
3939``` python
40-
40+ class Solution :
41+ def longestCommonPrefix (self , strs : List[str ]) -> str :
42+ n = len (strs)
43+ if n == 0 :
44+ return ' '
45+ for i in range (len (strs[0 ])):
46+ for j in range (1 , n):
47+ if len (strs[j]) <= i or strs[j][i] != strs[0 ][i]:
48+ return strs[0 ][:i]
49+ return strs[0 ]
4150```
4251
4352### ** Java**
4453
4554<!-- 这里可写当前语言的特殊实现逻辑 -->
4655
4756``` java
57+ class Solution {
58+ public String longestCommonPrefix (String [] strs ) {
59+ int n;
60+ if ((n = strs. length) == 0 ) return " " ;
61+ for (int i = 0 ; i < strs[0 ]. length(); ++ i) {
62+ for (int j = 1 ; j < n; ++ j) {
63+ if (strs[j]. length() <= i || strs[j]. charAt(i) != strs[0 ]. charAt(i)) {
64+ return strs[0 ]. substring(0 , i);
65+ }
66+ }
67+ }
68+ return strs[0 ];
69+ }
70+ }
71+ ```
4872
73+ ### ** C++**
74+
75+ ``` cpp
76+ class Solution {
77+ public:
78+ string longestCommonPrefix(vector<string >& strs) {
79+ int n;
80+ if ((n = strs.size()) == 0) return "";
81+ for (int i = 0; i < strs[ 0] .size(); ++i) {
82+ for (int j = 1; j < n; ++j) {
83+ if (strs[ j] .size() <= i || strs[ j] [ i ] != strs[ 0] [ i ] ) {
84+ return strs[ 0] .substr(0, i);
85+ }
86+ }
87+ }
88+ return strs[ 0] ;
89+ }
90+ };
4991```
5092
5193### **...**
Original file line number Diff line number Diff line change 4141### ** Python3**
4242
4343``` python
44-
44+ class Solution :
45+ def longestCommonPrefix (self , strs : List[str ]) -> str :
46+ n = len (strs)
47+ if n == 0 :
48+ return ' '
49+ for i in range (len (strs[0 ])):
50+ for j in range (1 , n):
51+ if len (strs[j]) <= i or strs[j][i] != strs[0 ][i]:
52+ return strs[0 ][:i]
53+ return strs[0 ]
4554```
4655
4756### ** Java**
4857
4958``` java
59+ class Solution {
60+ public String longestCommonPrefix (String [] strs ) {
61+ int n;
62+ if ((n = strs. length) == 0 ) return " " ;
63+ for (int i = 0 ; i < strs[0 ]. length(); ++ i) {
64+ for (int j = 1 ; j < n; ++ j) {
65+ if (strs[j]. length() <= i || strs[j]. charAt(i) != strs[0 ]. charAt(i)) {
66+ return strs[0 ]. substring(0 , i);
67+ }
68+ }
69+ }
70+ return strs[0 ];
71+ }
72+ }
73+ ```
5074
75+ ### ** C++**
76+
77+ ``` cpp
78+ class Solution {
79+ public:
80+ string longestCommonPrefix(vector<string >& strs) {
81+ int n;
82+ if ((n = strs.size()) == 0) return "";
83+ for (int i = 0; i < strs[ 0] .size(); ++i) {
84+ for (int j = 1; j < n; ++j) {
85+ if (strs[ j] .size() <= i || strs[ j] [ i ] != strs[ 0] [ i ] ) {
86+ return strs[ 0] .substr(0, i);
87+ }
88+ }
89+ }
90+ return strs[ 0] ;
91+ }
92+ };
5193```
5294
5395### **...**
Original file line number Diff line number Diff line change 1- static int x=[](){
2- std::ios::sync_with_stdio (false );
3- cin.tie (NULL );
4- return 0 ;
5- }();
6- string Compare (string s1,string s2)
7- {
8- if (s1.size ()==0 ||s2.size ()==0 )
9- return " " ;
10- int num=s1.size ()<s2.size ()?s1.size ():s2.size ();
11- string s;
12- for (int i=0 ;i<num;i++)
13- {
14- if (s1[i]==s2[i])
15- {
16- s.push_back (s1[i]);
17- }
18- else
19- break ;
20-
21- }
22- return s;
23- }
241class Solution {
252public:
263 string longestCommonPrefix (vector<string>& strs) {
27- if (strs.size ()==0 )
28- return " " ;
29- string prefix=strs[0 ];
30- for (int i=1 ;i<strs.size ();i++)
31- {
32- prefix=Compare (prefix,strs[i]);
4+ int n;
5+ if ((n = strs.size ()) == 0 ) return " " ;
6+ for (int i = 0 ; i < strs[0 ].size (); ++i) {
7+ for (int j = 1 ; j < n; ++j) {
8+ if (strs[j].size () <= i || strs[j][i] != strs[0 ][i]) {
9+ return strs[0 ].substr (0 , i);
10+ }
11+ }
3312 }
34- return prefix;
35-
13+ return strs[0 ];
3614 }
37- };
15+ };
Original file line number Diff line number Diff line change 11class Solution {
22 public String longestCommonPrefix (String [] strs ) {
3- if (strs == null || strs .length == 0 ) {
4- return "" ;
5- }
6- if (strs .length == 1 ) {
7- return strs [0 ];
8- }
9-
10- char [] chars = strs [0 ].toCharArray ();
11- int i = 0 ;
12- boolean flag = true ;
13- for (; i < chars .length ; ++i ) {
14- char ch = chars [i ];
15-
16- for (int j = 1 ; j < strs .length ; ++j ) {
17- if (strs [j ].length () <= i ) {
18- flag = false ;
19- break ;
3+ int n ;
4+ if ((n = strs .length ) == 0 ) return "" ;
5+ for (int i = 0 ; i < strs [0 ].length (); ++i ) {
6+ for (int j = 1 ; j < n ; ++j ) {
7+ if (strs [j ].length () <= i || strs [j ].charAt (i ) != strs [0 ].charAt (i )) {
8+ return strs [0 ].substring (0 , i );
209 }
21- if (strs [j ].charAt (i ) != ch ) {
22- flag = false ;
23- break ;
24- }
25-
26- }
27- if (!flag ) {
28- break ;
2910 }
3011 }
31- return strs [0 ].substring (0 , i );
32-
33-
12+ return strs [0 ];
3413 }
3514}
Original file line number Diff line number Diff line change 11class Solution :
2- def longestCommonPrefix (self , strs ):
3- """
4- :type strs: List[str]
5- :rtype: str
6- """
7- if strs == []:
2+ def longestCommonPrefix (self , strs : List [str ]) -> str :
3+ n = len (strs )
4+ if n == 0 :
85 return ''
9- if len (strs ) == 1 :
10- return strs [0 ]
11- strs .sort (key = lambda x : len (x ))
12- flag = False
13- prefix = ''
14- for j in range (1 ,len (strs [0 ])+ 1 ):
15- prefix = strs [0 ][:j ]
16- for i in range (1 ,len (strs )):
17- if prefix == strs [i ][:j ]:
18- flag = True
19- else :
20- flag = False
21- if not flag :
22- prefix0 = prefix [:- 1 ]
23- return prefix0
24- break
25- return prefix
6+ for i in range (len (strs [0 ])):
7+ for j in range (1 , n ):
8+ if len (strs [j ]) <= i or strs [j ][i ] != strs [0 ][i ]:
9+ return strs [0 ][:i ]
10+ return strs [0 ]
You can’t perform that action at this time.
0 commit comments