1. 程式人生 > >leetcode (Longest Uncommon Subsequence I)

leetcode (Longest Uncommon Subsequence I)

Title:Longest Uncommon Subsequence I     521

Difficulty:Easy

原題leetcode地址:https://leetcode.com/problems/longest-uncommon-subsequence-i/

 

1.    註解見程式碼註釋

時間複雜度:O(1)。

空間複雜度:O(1),沒有申請額外空間。

    /**
     * a和b一樣,則返回-1;a和b的長度誰大,返回誰的長度,否則返回a的長度
     * @param a
     * @param b
     * @return
     */
    public static int findLUSlength(String a, String b) {

        int aLen = a.length();
        int bLen = b.length();

        if (aLen != bLen) {
            return Math.max(aLen, bLen);
        }
        if (a == b) {
            return -1;
        }

        return aLen;

    }