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`

0 commit comments

Comments
 (0)