Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode
014.longest_common_prefix (Easy)
links:
Title: https://oj.leetcode.com/problems/longest-common-prefix/
Code (GitHub): Https://github.com/illuz/leetcode
Test Instructions:
The longest common prefix for multiple strings.
Analysis:
One judge can, no pit point.
Code:
C++:
Class Solution {public: string Longestcommonprefix (vector<string> &strs) {if (Strs.empty ()) return ""; for (int i = 0; i < strs[0].length (), i++) {for (int j = 1; J < strs.size (); + j) if (I >= strs[j].length () | | str S[j][i]! = Strs[0][i]) return strs[0].substr (0, i);} return strs[0];} ;
Java:
public class Solution {public String longestcommonprefix (string[] strs) { if (strs.length = = 0) return "";
for (int i = 0; i < strs[0].length (); i++) {for (int j = 1; j < Strs.length; J + +) if (Strs[j].length () & lt;= I | | Strs[j].charat (i)! = Strs[0].charat (i)) return strs[0].substring (0, I); } return strs[0];} }
Python:
Class solution: # @return A string def longestcommonprefix (self, STRs): if STRs = = []: return ' for I in range (len (Strs[0])): For str in STRs: if Len (str) <= I or str[i]! = Strs[0][i]: return strs[0][:i]
return Strs[0]
[Leetcode] 014. Longest Common Prefix (easy) (C++/java/python)