Longest Common Pefix

来源:互联网 发布:全新英朗刷启停软件 编辑:程序博客网 时间:2024/04/25 12:05

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

代码

public class Solution {    public static String longestCommonPrefix(String[] strs) {        int temp = 0;        String result = "";        ok: for (int i = 0; i < strs[0].length();)            for (int j = 1; j < strs.length; j++) {                if (i < strs[j].length() && strs[0].charAt(i) == strs[j].charAt(i)) {                    temp++;                }                if (temp != j) {                    break ok;                }                if (temp == strs.length - 1) {                    result = result + strs[0].charAt(i);                    temp = 0;                    i++;                }            }        return result;    }}

【注意】加入识别字跳出多重循环

0 0
原创粉丝点击