[LeetCode-interview algorithm classic-Java implementation] [014-Longest Common Prefix (Longest Common Prefix)], leetcode -- java
[014-Longest Common Prefix (Longest Common Prefix )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question
Write a function to find the longest common prefix string amongst an array of strings.
Theme
Write a function to find the longest public prefix in the array of a string.
Solutions
The first step is to first find the minimum length of the string, and then compare the string with other strings to find the shortest most common prefix.
Code Implementation
Public class Solution {public String longestCommonPrefix (String [] strs) {if (strs = null) {return null;} if (strs. length = 0) {return "";} int min = Integer. MAX_VALUE; // record the length of the shortest String // find the length of the short String for (String str: strs) {if (str = null) {return null ;} if (min> str. length () {min = str. length () ;}} int I; // The maximum number of characters in the record prefix boolean flag; for (I = 0; I <min; I ++) {flag = true; for (int j = 1; j <strs. length; j ++) {if (strs [0]. charAt (I )! = Strs [j]. charAt (I) {flag = false; break ;}} if (! Flag) {break; }}// if (I = 0) {// return null; //} return strs [0]. substring (0, I );}}
Evaluation Result
Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Note
Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/46963133]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.