Title:Write a function to find the longest common prefix string amongst an array of strings.
Translation: asks for a common longest prefix in a string array.
Train of thought: Take the first string as the benchmark, traverse the position by location, and iterate through the string array, if a string length is smaller than the current position, or the character of the current position is different, return string strs[0].substring (0,pos);
Code:
public string Longestcommonprefix (string[] strs) { int count = strs.length;//The number of strings int pos = 0;//current pointer position if ( Count = = 0| | Strs[0].length () ==0)//judgment null return ""; For (Pos < Strs[0].length ();p os++) {for (int j = 1; j < count;j++) { if (strs[j].length () <=pos || Strs[0].charat (POS)!=strs[j].charat (POS))//over length, or unequal return strs[0].substring (0, POS); return string } } return strs[0]; }
Leetcode longest Common Prefix longest prefix