【LeetCode从零单排】No14.LongestCommonPrefix

来源:互联网 发布:淘宝规则的划分 编辑:程序博客网 时间:2024/04/29 04:31

题目

    Write a function to find the longest common prefix string amongst an array of strings.

代码

public class Solution {    public String longestCommonPrefix(String[] strs) {        if(strs.length==0) return "";        if(strs.length==1) return strs[0];   String commonPrefix="";       boolean end=true;             char strChar[]=strs[0].toCharArray();        ok:        for (int j = 0;j<strChar.length;j++){                 for(int i=1;i<strs.length;i++){           char tempChar[]=strs[i].toCharArray();           int tempChar_length=tempChar.length;           if(j<tempChar_length){             if(tempChar[j]==strChar[j]){                         }             else{                      break ok;                            }             }           else{           break ok;           }                                }           commonPrefix+=strChar[j];    }               return commonPrefix;    }}

代码下载:https://github.com/jimenbian/GarvinLeetCode


/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/



0 0
原创粉丝点击