題目

題目連結:https://leetcode.com/problems/text-justification/

給很多個字串和一個寬度 maxWidth,將字串逐一放入並讓每一行都是 maxWidth 寬。

若是最後一行、或是一行內只有一個字串,則靠左排列。其餘則置中排列,平均的分配空白到字串之間。

置中排列時若空白的數量不能平均的被分配,則左邊的空格會比右邊的空格多一個。

範例說明

Example 1:

1
2
3
4
5
6
7
8
9
Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
"This is an",
"example of text",
"justification. "
]

Example 2:

1
2
3
4
5
6
7
8
9
10
11
12
Input:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
Output:
[
"What must be",
"acknowledgment ",
"shall be "
]
Explanation: Note that the last line is "shall be " instead of "shall be",
because the last line must be left-justified instead of fully-justified.
Note that the second line is also left-justified becase it contains only one word.

Example 3:

1
2
3
4
5
6
7
8
9
10
11
12
13
Input:
words = ["Science","is","what","we","understand","well","enough","to","explain",
"to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
Output:
[
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do "
]
閱讀全文 »

題目

題目連結:https://leetcode.com/problems/insert-interval/

給定一個集合的區間,插入一個新的區間並輸出結果。已知給定的區間都不重疊且已經由左到右排好。

範例說明

Example 1:

1
2
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]

Example 2:

1
2
3
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
閱讀全文 »

題目

題目連結:https://leetcode.com/problems/jump-game-ii/submissions/

給一非負的整數序列,一開始在序列的第一格上。序列的值代表在此位置時最多可以跳多少距離,求最少幾步可以到達序列的最後一格。

範例說明

1
2
3
4
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
閱讀全文 »

題目

題目連結:https://leetcode.com/problems/wildcard-matching/

給一個字串 s 和樣板(pattern) p,實作支援 '?''*' 的 wildcard pattern matching。

1
2
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).

計算 s 是否匹配 p

範例說明

Example 1:

1
2
3
4
5
Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".

Example 2:

1
2
3
4
5
Input:
s = "aa"
p = "*"
Output: true
Explanation: '*' matches any sequence.

Example 3:

1
2
3
4
5
Input:
s = "cb"
p = "?a"
Output: false
Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.

Example 4:

1
2
3
4
5
Input:
s = "adceb"
p = "*a*b"
Output: true
Explanation: The first '*' matches the empty sequence, while the second '*' matches the substring "dce".

Example 5:

1
2
3
4
Input:
s = "acdcb"
p = "a*c?b"
Output: false
閱讀全文 »
0%