Skip to content

Commit 4614f58

Browse files
committed
Micro fix
1 parent a2ceb13 commit 4614f58

File tree

89 files changed

+323
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+323
-229
lines changed

leetcode/0547.Friend-Circles/README.md renamed to leetcode/0547.Number-of-Provinces/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [547. Friend Circles](https://leetcode.com/problems/friend-circles/)
1+
# [547. Number of Provinces](https://leetcode.com/problems/number-of-provinces/)
22

33
## 题目
44

Original file line numberDiff line numberDiff line change
@@ -1,28 +1,101 @@
1-
# [828. Unique Letter String](https://leetcode.com/problems/unique-letter-string/)
1+
# [828. Count Unique Characters of All Substrings of a Given String](https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/)
2+
23

34
## 题目
45

5-
THIS PROBLEM COPYRIGHT BELONGS TO CODILITY.COM
6+
Let's define a function `countUniqueChars(s)` that returns the number of unique characters on `s`, for example if `s = "LEETCODE"` then `"L"``"T"`,`"C"`,`"O"`,`"D"` are the unique characters since they appear only once in `s`, therefore `countUniqueChars(s) = 5`.On this problem given a string `s` we need to return the sum of `countUniqueChars(t)` where `t` is a substring of `s`. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
7+
8+
Since the answer can be very large, return the answer modulo `10 ^ 9 + 7`.
69

710
**Example 1:**
811

12+
```
13+
Input: s = "ABC"
14+
Output: 10
15+
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
16+
Evey substring is composed with only unique letters.
17+
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
918
19+
```
1020

1121
**Example 2:**
1222

23+
```
24+
Input: s = "ABA"
25+
Output: 8
26+
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
27+
28+
```
29+
30+
**Example 3:**
31+
32+
```
33+
Input: s = "LEETCODE"
34+
Output: 92
1335
36+
```
37+
38+
**Constraints:**
39+
40+
- `0 <= s.length <= 10^4`
41+
- `s` contain upper-case English letters only.
1442

1543
## 题目大意
1644

1745
如果一个字符在字符串 S 中有且仅有出现一次,那么我们称其为独特字符。例如,在字符串 S = "LETTER" 中,"L" 和 "R" 可以被称为独特字符。我们再定义 UNIQ(S) 作为字符串 S 中独特字符的个数。那么,在 S = "LETTER" 中, UNIQ("LETTER") =  2。
1846

1947
对于给定字符串 S,计算其所有非空子串的独特字符的个数(即 UNIQ(substring))之和。如果在 S 的不同位置上出现两个甚至多个相同的子串,那么我们认为这些子串是不同的。考虑到答案可能会非常大,规定返回格式为:结果 mod 10 ^ 9 + 7。
2048

21-
2249
## 解题思路
2350

2451
- 这一题可以先用暴力解法尝试解题,不过提交以后会发现判题结果是超时。出错的一组数据是一个有 10000 个字符的字符串。暴力解法中间由于遍历了太多的子区间,导致了超时。
2552
- 这道题换一个角度思考问题。当子字符串中字符 X 出现了 2 次以上,那么它就对最终结果没有任何影响,所以只有当某个字符只出现一次的时候才会影响最终结果。再者,一个子字符串中不重复的字符的总个数,也就是这个子字符串 UNIQ 值。例如,“ABC”,这个子字符串的 UNIQ 值是 3,可以这样计算,它属于 A 的独特的字符串,也属于 B 的独特的字符串,也属于 C 的独特的字符串,那么计算这个子字符串的问题可以分解成计算 A 有多少个独特的子字符串,B 有多少个独特的子字符串,C 有多少个独特的子字符串的问题。在计算 A 有多少个子字符串的问题的时候,里面肯定会包含 "ABC" 这个子字符串的。所以原问题就转换成了分别计算给出的字符串中每个字符出现在独特字符串中的总数之和。
26-
- 假设原字符串是 BAABBABBBAAABA,这个字符串中出现了很多 A 和很多 B,假设我们当前计算到了第 3 个 A 的位置了(index = 5),即标红色的那个 A。如何计算这个 A 在哪些子字符串中是独特的呢?由于子字符串题目中要求必须是连续的区间,所以这个问题很简单。找到这个 A 前一个 A 的下标位置(index = 2),再找到这个 A 后一个 A 的下标位置(index = 9),即 BAABBABBBAAABA,第一个 A 和当前计算的 A 中间区间有 2 个字符,第三个 A 和当前计算的 A 中间有 3 个字符。那么当前计算的 A 出现在 `(2 + 1) * (3 + 1) = 12` 个子字符串中是独特的,这 12 个字符串是:`A`,`BA`,`BBA`,`AB`,`ABB`,`ABBB`,`BAB`,`BABB`,`BABBB`,`BBAB`,`BBABB`,`BBABBB`。计算方法,假设当前待计算的字符的下标是 i ,找到当前字符前一次出现的下标位置 left,再找到当前字符后一次出现的下标位置 right,那么左边区间 (left,i) 的**开区间**内包含的字符数是 i - left - 1,右边区间 (i,right) 的**开区间**内包含的字符数是 right - i - 1。左右两边都还需要考虑空字符串的情况,即左右两边都可以不取任何字符,那么对应的就是只有中间这个待计算的字符 `A`。所以左右两边都还需要再加上空串的情况,左边 i - left - 1 + 1 = i - left,右边 right - i - 1 + 1 = right - i。左右两边的情况进行排列组合,即 (i - left) * (right - i)。针对字符串的每个字符都计算这样的值,最后累积的总和就是题目中要求的总 UNIQ 值。
27-
28-
53+
- 假设原字符串是 BAABBABBBAAABA,这个字符串中出现了很多 A 和很多 B,假设我们当前计算到了第 3 个 A 的位置了(index = 5),即标红色的那个 A。如何计算这个 A 在哪些子字符串中是独特的呢?由于子字符串题目中要求必须是连续的区间,所以这个问题很简单。找到这个 A 前一个 A 的下标位置(index = 2),再找到这个 A 后一个 A 的下标位置(index = 9),即 BAABBABBBAAABA,第一个 A 和当前计算的 A 中间区间有 2 个字符,第三个 A 和当前计算的 A 中间有 3 个字符。那么当前计算的 A 出现在 `(2 + 1) * (3 + 1) = 12` 个子字符串中是独特的,这 12 个字符串是:`A`,`BA`,`BBA`,`AB`,`ABB`,`ABBB`,`BAB`,`BABB`,`BABBB`,`BBAB`,`BBABB`,`BBABBB`。计算方法,假设当前待计算的字符的下标是 i ,找到当前字符前一次出现的下标位置 left,再找到当前字符后一次出现的下标位置 right,那么左边区间 (left,i) 的***开区间****内包含的字符数是 i - left - 1,右边区间 (i,right) 的***开区间****内包含的字符数是 right - i - 1。左右两边都还需要考虑空字符串的情况,即左右两边都可以不取任何字符,那么对应的就是只有中间这个待计算的字符 `A`。所以左右两边都还需要再加上空串的情况,左边 i - left - 1 + 1 = i - left,右边 right - i - 1 + 1 = right - i。左右两边的情况进行排列组合,即 (i - left) * (right - i)。针对字符串的每个字符都计算这样的值,最后累积的总和就是题目中要求的总 UNIQ 值。
54+
55+
## 代码
56+
57+
```go
58+
package leetcode
59+
60+
func uniqueLetterString(S string) int {
61+
res, left, right := 0, 0, 0
62+
for i := 0; i < len(S); i++ {
63+
left = i - 1
64+
for left >= 0 && S[left] != S[i] {
65+
left--
66+
}
67+
right = i + 1
68+
for right < len(S) && S[right] != S[i] {
69+
right++
70+
}
71+
res += (i - left) * (right - i)
72+
}
73+
return res % 1000000007
74+
}
75+
76+
// 暴力解法,超时!时间复杂度 O(n^2)
77+
func uniqueLetterString1(S string) int {
78+
if len(S) == 0 {
79+
return 0
80+
}
81+
res, mod := 0, 1000000007
82+
for i := 0; i < len(S); i++ {
83+
letterMap := map[byte]int{}
84+
for j := i; j < len(S); j++ {
85+
letterMap[S[j]]++
86+
tmp := 0
87+
for _, v := range letterMap {
88+
if v > 1 {
89+
tmp++
90+
}
91+
}
92+
if tmp == len(letterMap) {
93+
continue
94+
} else {
95+
res += len(letterMap) - tmp
96+
}
97+
}
98+
}
99+
return res % mod
100+
}
101+
```

website/content/ChapterFour/0098.Validate-Binary-Search-Tree.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Assume a BST is defined as follows:
1111
- The right subtree of a node contains only nodes with keys **greater than** the node's key.
1212
- Both the left and right subtrees must also be binary search trees.
1313

14-
**xample 1:**
14+
**Example 1**:
1515

1616
2
1717
/ \

website/content/ChapterFour/0130.Surrounded-Regions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ After running your function, the board should be:
2222
X X X X
2323
X O X X
2424

25-
**Explanation:**
25+
**Explanation**:
2626

2727
Surrounded regions shouldn’t be on the border, which means that any `'O'` on the border of the board are not flipped to `'X'`. Any `'O'` that is not on the border and it is not connected to an `'O'` on the border will be flipped to `'X'`. Two cells are connected if they are adjacent cells connected horizontally or vertically.
2828

website/content/ChapterFour/0146.LRU-Cache.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Implement the `LRUCache` class:
1010
- `int get(int key)` Return the value of the `key` if the key exists, otherwise return `1`.
1111
- `void put(int key, int value)` Update the value of the `key` if the `key` exists. Otherwise, add the `key-value` pair to the cache. If the number of keys exceeds the `capacity` from this operation, **evict** the least recently used key.
1212

13-
**Follow up:**Could you do `get` and `put` in `O(1)` time complexity?
13+
**Follow up**:Could you do `get` and `put` in `O(1)` time complexity?
1414

15-
**Example 1:**
15+
**Example 1**:
1616

1717
```
1818
Input
@@ -35,7 +35,7 @@ lRUCache.get(4); // return 4
3535
3636
```
3737

38-
**Constraints:**
38+
**Constraints**:
3939

4040
- `1 <= capacity <= 3000`
4141
- `0 <= key <= 3000`

website/content/ChapterFour/0189.Rotate-Array.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
Given an array, rotate the array to the right by *k* steps, where *k* is non-negative.
66

7-
**Follow up:**
7+
**Follow up**:
88

99
- Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
1010
- Could you do it in-place with O(1) extra space?
1111

12-
**Example 1:**
12+
**Example 1**:
1313

1414
```
1515
Input: nums = [1,2,3,4,5,6,7], k = 3
@@ -20,7 +20,7 @@ rotate 2 steps to the right: [6,7,1,2,3,4,5]
2020
rotate 3 steps to the right: [5,6,7,1,2,3,4]
2121
```
2222

23-
**Example 2:**
23+
**Example 2**:
2424

2525
```
2626
Input: nums = [-1,-100,3,99], k = 2
@@ -30,7 +30,7 @@ rotate 1 steps to the right: [99,-1,-100,3]
3030
rotate 2 steps to the right: [3,99,-1,-100]
3131
```
3232

33-
**Constraints:**
33+
**Constraints**:
3434

3535
- `1 <= nums.length <= 2 * 10^4`
3636
- `-2^31 <= nums[i] <= 2^31 - 1`

website/content/ChapterFour/0228.Summary-Ranges.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Each range `[a,b]` in the list should be output as:
1212
- `"a->b"` if `a != b`
1313
- `"a"` if `a == b`
1414

15-
**Example 1:**
15+
**Example 1**:
1616

1717
```
1818
Input: nums = [0,1,2,4,5,7]
@@ -24,7 +24,7 @@ Explanation: The ranges are:
2424
2525
```
2626

27-
**Example 2:**
27+
**Example 2**:
2828

2929
```
3030
Input: nums = [0,2,3,4,6,8,9]
@@ -37,31 +37,31 @@ Explanation: The ranges are:
3737
3838
```
3939

40-
**Example 3:**
40+
**Example 3**:
4141

4242
```
4343
Input: nums = []
4444
Output: []
4545
4646
```
4747

48-
**Example 4:**
48+
**Example 4**:
4949

5050
```
5151
Input: nums = [-1]
5252
Output: ["-1"]
5353
5454
```
5555

56-
**Example 5:**
56+
**Example 5**:
5757

5858
```
5959
Input: nums = [0]
6060
Output: ["0"]
6161
6262
```
6363

64-
**Constraints:**
64+
**Constraints**:
6565

6666
- `0 <= nums.length <= 20`
6767
- `231 <= nums[i] <= 231 - 1`

website/content/ChapterFour/0460.LFU-Cache.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Implement the `LFUCache` class:
1313

1414
**Notice that** the number of times an item is used is the number of calls to the `get` and `put` functions for that item since it was inserted. This number is set to zero when the item is removed.
1515

16-
**Example 1:**
16+
**Example 1**:
1717

1818
```
1919
Input
@@ -37,12 +37,12 @@ lfu.get(4); // return 4
3737
3838
```
3939

40-
**Constraints:**
40+
**Constraints**:
4141

4242
- `0 <= capacity, key, value <= 104`
4343
- At most `10^5` calls will be made to `get` and `put`.
4444

45-
**Follow up:** Could you do both operations in `O(1)` time complexity?
45+
**Follow up**: Could you do both operations in `O(1)` time complexity?
4646

4747
## 题目大意
4848

website/content/ChapterFour/0493.Reverse-Pairs.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ Given an array `nums`, we call `(i, j)` an **important reverse pair** if `
77

88
You need to return the number of important reverse pairs in the given array.
99

10-
**Example1:**
10+
**Example1**:
1111

1212
Input: [1,3,2,3,1]
1313
Output: 2
1414

15-
**Example2:**
15+
**Example2**:
1616

1717
Input: [2,4,3,5,1]
1818
Output: 3

website/content/ChapterFour/0497.Random-Point-in-Non-overlapping-Rectangles.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Given a list of **non-overlapping** axis-aligned rectangles `rects`, write a
3131
Output:
3232
[null,[-1,-2],[2,0],[-2,-1],[3,0],[-2,-2]]
3333

34-
**Explanation of Input Syntax:**
34+
**Explanation of Input Syntax**:
3535

3636
The input is two lists: the subroutines called and their arguments. `Solution`'s constructor has one argument, the array of rectangles `rects``pick` has no arguments. Arguments are always wrapped with a list, even if there aren't any.
3737

website/content/ChapterFour/0528.Random-Pick-with-Weight.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Given an array `w` of positive integers, where `w[i]` describes the weight o
2525
[[[1,3]],[],[],[],[],[]]
2626
Output: [null,0,1,1,1,0]
2727

28-
**Explanation of Input Syntax:**
28+
**Explanation of Input Syntax**:
2929

3030
The input is two lists: the subroutines called and their arguments. `Solution`'s constructor has one argument, the array `w``pickIndex` has no arguments. Arguments are always wrapped with a list, even if there aren't any.
3131

website/content/ChapterFour/0541.Reverse-String-II.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Given a string and an integer k, you need to reverse the first k characters for
1010
Input: s = "abcdefg", k = 2
1111
Output: "bacdfeg"
1212

13-
**Restrictions:**
13+
**Restrictions**:
1414

1515
1. The string consists of lower English letters only.
1616
2. Length of the given string and k will in the range [1, 10000]

website/content/ChapterFour/0547.Friend-Circles.md renamed to website/content/ChapterFour/0547.Number-of-Provinces.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [547. Friend Circles](https://leetcode.com/problems/friend-circles/)
1+
# [547. Number of Provinces](https://leetcode.com/problems/number-of-provinces/)
22

33
## 题目
44

website/content/ChapterFour/0605.Can-Place-Flowers.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ You have a long flowerbed in which some of the plots are planted, and some are n
66

77
Given an integer array `flowerbed` containing `0`'s and `1`'s, where `0` means empty and `1` means not empty, and an integer `n`, return *if* `n` new flowers can be planted in the `flowerbed` without violating the no-adjacent-flowers rule.
88

9-
**Example 1:**
9+
**Example 1**:
1010

1111
```
1212
Input: flowerbed = [1,0,0,0,1], n = 1
1313
Output: true
1414
```
1515

16-
**Example 2:**
16+
**Example 2**:
1717

1818
```
1919
Input: flowerbed = [1,0,0,0,1], n = 2
2020
Output: false
2121
```
2222

23-
**Constraints:**
23+
**Constraints**:
2424

2525
- `1 <= flowerbed.length <= 2 * 104`
2626
- `flowerbed[i]` is `0` or `1`.

website/content/ChapterFour/0785.Is-Graph-Bipartite.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The graph is given in the following form: `graph[i]` is a list of indexes `j`
3232
We cannot find a way to divide the set of nodes into two independent subsets.
3333

3434

35-
**Note:**
35+
**Note**:
3636

3737
- `graph` will have length in range `[1, 100]`.
3838
- `graph[i]` will contain integers in range `[0, graph.length - 1]`.

0 commit comments

Comments
 (0)