[LeetCode-interview algorithm classic-Java implementation] [014-Longest Common Prefix (Longest Common Prefix)], leetcode -- java

Source: Internet
Author: User

[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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.