1. 程式人生 > >[日常刷題]leetcode D42

[日常刷題]leetcode D42

521. Longest Uncommon Subsequence I

Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.

A subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.

The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn’t exist, return -1.

Example 1:

Input: "aba", "cdc"
Output: 3
Explanation: The longest uncommon subsequence is "aba" (or "cdc"), 
because "aba" is a subsequence of "aba", 
but not a subsequence of any other strings in the group of two strings.

Note:

  1. Both strings’ lengths will not exceed 100.
  2. Only letters from a ~ z will appear in input strings.

Solution in C++:

關鍵點:

  • 分類討論

思路:

  1. 哇,開始還以為很難,最開始寫的程式碼只是處理一下我認為的特殊情況,然後在想其他情況的case的時候,我想了幾個,比如acb與ab,我以為會輸出2,但是結果不是,我就想到了因為前面不相等直接輸出其中的長度最大值了,然後再就想不出來能跨過這個的例子了,打算submit一下,看有沒有錯誤提示,結果AC了,滿臉震驚,然後去看題解居然也是這樣做的,看了分析才認識到真正的原理。真是分析大法好啊。
    解析
int findLUSlength(string a, string b) {
        if (a != b)
            return max(a.size(), b.size());
        else
            return -1;
    }

551. Student Attendance Record I

You are given a string representing an attendance record for a student. The record only contains the following three characters:

  1. ’A’ : Absent.
  2. ’L’ : Late.
  3. ’P’ : Present.
    A student could be rewarded if his attendance record doesn’t contain more than one ‘A’ (absent) or more than two continuous ‘L’ (late).

You need to return whether the student could be rewarded according to his attendance record.

Example 1:

Input: "PPALLP"
Output: True

Example 2:

Input: "PPALLL"
Output: False

Solution in C++:

關鍵點:

  • 連續的L

思路:

  • 開始沒看到連續,然後老是錯,還以為自己邏輯看錯了,後來發現是題意理解錯了(不過也是通過例子才知道的),接下來就是連續L計數的問題,開始把不是連續L的值設定為0出現了邏輯問題,後來就把不是連續的L設定成1就解決了。大框架就是遍歷就好。然後可能這裡還有就是a和l的值判斷的時機應該放在改變值之後,以免遇到字串結尾才出現False的情況。
  • 看到discuss裡面判斷連續L的邏輯是碰到P之後將l的值歸零,其他情況正常+1,感覺這個邏輯也蠻不錯的,點個贊。
bool checkRecord(string s) {
        int a = 0;
        int l = 0;
        
        for(int i = 0; i < s.size(); ++i){
            if (s[i] == 'A')
                ++a;
            else if (s[i] == 'L'){
                if (i != 0 && s[i-1] == 'L')
                    ++l;
                else
                    l = 1;
            }
            if (a > 1)
                return false;
            if (l > 2)
                return false;
        }
        
        return true;
    }

小結

今天實驗室出去聚會了,回來比較晚了,明天還有課還要去實驗室打卡,今天就刷不到那麼長時間了,但是不過卻有不少收穫,思維上的鍛鍊得到了,很享受。

知識點

  • 分類討論

相關推薦

[日常]leetcode D42

521. Longest Uncommon Subsequence I Given a group of two strings, you need to find the longest uncommon subsequence of this group

[日常]leetcode第十四天

155. Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push el

[日常]leetcode D37

475. Heaters Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all

[日常]leetcode D33

414. Third Maximum Number Given a non-empty array of integers, return the third maximum number in this array. If it does not exist

[日常]leetcode D39

492. Construct the Rectangle For a web developer, it is very important to know how to design a web page’s size. So, given a specif

[日常]leetcode D29

367. Valid Perfect Square Given a positive integer num, write a function which returns True if num is a perfect square else False.

[日常]leetcode D34

441. Arranging Coins You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactl

LeetCode日常110

110. 平衡二叉樹給定一個二叉樹,判斷它是否是高度平衡的二叉樹。本題中,一棵高度平衡二叉樹定義為:一個二叉樹每個節點 的左右兩個子樹的高度差的絕對值不超過1。示例 1:給定二叉樹 [3,9,20,null,null,15,7] 3 / \ 9 20

LeetCode日常542、

542. 01 矩陣給定一個由 0 和 1 組成的矩陣,找出每個元素到最近的 0 的距離。兩個相鄰元素間的距離為 1 。示例 1: 輸入:0 0 0 0 1 0 0 0 0 輸出:0 0 0 0 1 0 0 0 0 示例 2: 輸入:0 0 0 0 1 0 1 1 1 輸出:

LeetCode日常542

542. 01 矩陣給定一個由 0 和 1 組成的矩陣,找出每個元素到最近的 0 的距離。兩個相鄰元素間的距離為 1 。示例 1: 輸入:0 0 0 0 1 0 0 0 0 輸出:0 0 0 0 1 0

LeetCode日常605

605. 種花問題假設你有一個很長的花壇,一部分地塊種植了花,另一部分卻沒有。可是,花卉不能種植在相鄰的地塊上,它們會爭奪水源,兩者都會死去。給定一個花壇(表示為一個數組包含0和1,其中0表示沒種植花,

LeetCode日常504、682

504. 七進位制數給定一個整數,將其轉化為7進位制,並以字串形式輸出。示例 1:輸入: 100 輸出: "202" 示例 2:輸入: -7 輸出: "-10" 注意: 輸入範圍是 [-1e7, 1e7

LeetCode日常

693. 交替位二進位制數給定一個正整數,檢查他是否為交替位二進位制數:換句話說,就是他的二進位制數相鄰的兩個位數永不相等。示例 1:輸入: 5 輸出: True 解釋: 5的二進位制數是: 101 示

Java日常第十一天

選擇題 1.類 ABC 定義如下: 1 . public class ABC{ 2 . public double max( double a, double b) { } 3 . 4 . } 將以下哪個方法插入行 3 是不合法的。(B) A.pu

Java日常第十天

選擇題 1.在使用super和this關鍵字時,在子類構造方法中使用super()顯示呼叫父類的構造方法,super()必須寫在子類構造方法的第一行,否則編譯不通過. 解析:1)呼叫super()必須寫在子類構造方法的第一行,否則編譯不通過。每個子類構造方法的第一條語句,都是隱含地呼叫s

Java日常第九天

1.java7後關鍵字 switch 支不支援字串作為條件:(錯) 解析:在Java7之前,Switch支援的條件型別: byte,short,chart,int,enum以及基本型別的封裝類,在Java7之後,開始支援字串型別。 2.佇列(Queue)是先進先出的。(對) 3.This呼叫

Java日常第七天

選擇題 1.在類Tester中定義方法如下, public double max(int x, int y) { // 省略 } 則在該類中定義如下哪個方法頭是對上述方法的過載(Overload)?(B) A.public int max(int a, int b) {} B.p

Java日常第六天

一、選擇題 1.下列關於Java語言的特點,描述錯誤的是(C。Java是面向過程的程式語言) A。Java是跨平臺的程式語言 B。Java支援分散式計算 C。Java是面向(物件)的程式語言 D。Java支援多執行緒 2.下列那個類的宣告是正確的?(D) A。abstract final

日常】NOIP練習題題解

NOIP練習題題解 1.小X與位運算 2.小x與機器人 3.賽車 4.whatbase 5.height 6.最優分解 7.angry 1.小X與位運算 題目描述 自從上次小X

日常】[SCOI2005]掃雷(多維動態規劃)

[SCOI2005]掃雷 我們對於這道題,我們可以採用DP的方法來解決。 我們設f[i][0/1][0/1][0/1]表示滿足前i個條件限制,第i-1,i,i+1分別放(1)和不放(0)的方案數。 對於a[i](第二列的數字),我們採用分類討論的方法來進行狀態轉移。 當a[i