LongestCommonPrefix

  • 2022-12-14
  • 浏览 (508)

LongestCommonPrefix.java 源码

package datastructure.array.leetcode;

/**
 * 最长公共前缀
 */
public class LongestCommonPrefix {

    /**
     * 纵向扫描
     */
    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }

        int length = strs[0].length();
        int count = strs.length;
        for (int i = 0; i < length; i++) {
            char c = strs[0].charAt(i);
            for (int j = 1; j < count; j++) {
                if (i == strs[j].length() || strs[j].charAt(i) != c) {
                    return strs[0].substring(0, i);
                }
            }
        }
        return strs[0];
    }
}

你可能感兴趣的文章

BestTimeBuySellStock

BestTimeBuySellStockII

BullsAndCows

0  赞