码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode-Longest Common Prefix

时间:2016-04-05 07:09:17      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

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==null && strs.length()==0){
      return "";
    }
    int len=strs.length();
String pre=strs[0]; for(int i=1; i<len; i++){ int j=0; while(j<pre.length() && j<strs[i].length() && strs[i].charAt(j)==pre.charAt(j)){ j++; } if(j==0){ return ""; } pre=pre.substring(0, j); } return pre; } }

 

LeetCode-Longest Common Prefix

标签:

原文地址:http://www.cnblogs.com/incrediblechangshuo/p/5353640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!