Topic:
Write a function to find the longest common prefix string amongst an array of strings.
Ideas:
Base the first string, and then cycle through the comparison
Packagestring; Public classLongestcommonprefix { PublicString Longestcommonprefix (string[] strs) {intLen = 0; if(STRs = =NULL|| (len = strs.length) = = 0)return""; StringBuilder SB=NewStringBuilder (); inti = 0; intLen0 = Strs[0].length (); while(I <len0) { Charc = strs[0].charat (i); for(intj = 1; J < Len; ++j) {if(I >= strs[j].length () | | Strs[j].charat (i)! =c)returnsb.tostring (); } sb.append (c); ++i; } returnsb.tostring (); } Public Static voidMain (string[] args) {//TODO auto-generated Method StubString[] STRs = {"abc", "AB", "ABCD", "abcdef" }; Longestcommonprefix L=NewLongestcommonprefix (); System.out.println (L.longestcommonprefix (STRs)); }}
Leetcode-longest Common Prefix