Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b48104d

Browse files
Merge pull request SharingSource#668 from SharingSource/ac_oier
✨feat: add 667、828
2 parents bb2e249 + b7e440d commit b48104d

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed

‎Index/数学.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
| [789. 逃脱阻碍者](https://leetcode-cn.com/problems/escape-the-ghosts/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/escape-the-ghosts/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-w69gr/) | 中等 | 🤩🤩🤩🤩🤩 |
5151
| [793. 阶乘函数后 K 个零](https://leetcode.cn/problems/preimage-size-of-factorial-zeroes-function/) | [LeetCode 题解链接](https://leetcode.cn/problems/preimage-size-of-factorial-zeroes-function/solution/by-ac_oier-pk9g/) | 困难 | 🤩🤩🤩🤩 |
5252
| [810. 黑板异或游戏](https://leetcode-cn.com/problems/chalkboard-xor-game/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/chalkboard-xor-game/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-ges7k/) | 困难 | 🤩🤩🤩🤩 |
53+
| [828. 统计子串中的唯一字符](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/solution/by-ac_oier-922k/) | 困难 | 🤩🤩🤩🤩 |
5354
| [829. 连续整数求和](https://leetcode.cn/problems/consecutive-numbers-sum/) | [LeetCode 题解链接](https://leetcode.cn/problems/consecutive-numbers-sum/solution/by-ac_oier-220q/) | 困难 | 🤩🤩🤩🤩 |
5455
| [869. 重新排序得到 2 的幂](https://leetcode-cn.com/problems/reordered-power-of-2/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reordered-power-of-2/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-c-3s1e/) | 中等 | 🤩🤩🤩🤩 |
5556
| [879. 盈利计划](https://leetcode-cn.com/problems/profitable-schemes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/profitable-schemes/solution/gong-shui-san-xie-te-shu-duo-wei-fei-yon-7su9/) | 困难 | 🤩🤩🤩🤩🤩 |

‎Index/构造.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
33
| [324. 摆动排序 II](https://leetcode.cn/problems/wiggle-sort-ii/) | [LeetCode 题解链接](https://leetcode.cn/problems/wiggle-sort-ii/solution/by-ac_oier-22bq/) | 中等 | 🤩🤩🤩🤩 |
44
| [406. 根据身高重建队列](https://leetcode.cn/problems/queue-reconstruction-by-height/) | [LeetCode 题解链接](https://leetcode.cn/problems/queue-reconstruction-by-height/solution/by-ac_oier-fda2/) | 中等 | 🤩🤩🤩🤩 |
5+
| [667. 优美的排列 II](https://leetcode.cn/problems/beautiful-arrangement-ii/) | [LeetCode 题解链接](https://leetcode.cn/problems/beautiful-arrangement-ii/solution/by-ac_oier-lyns/) | 中等 | 🤩🤩🤩🤩🤩 |
56
| [761. 特殊的二进制序列](https://leetcode.cn/problems/special-binary-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/special-binary-string/solution/by-ac_oier-cz6h/) | 困难 | 🤩🤩🤩🤩🤩 |
67
| [782. 变为棋盘](https://leetcode.cn/problems/transform-to-chessboard/) | [LeetCode 题解链接](https://leetcode.cn/problems/transform-to-chessboard/solution/by-ac_oier-vf1m/) | 困难 | 🤩🤩🤩🤩 |
78
| [899. 有序队列](https://leetcode.cn/problems/orderly-queue/) | [LeetCode 题解链接](https://leetcode.cn/problems/orderly-queue/solution/by-ac_oier-443m/) | 困难 | 🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
| [819. 最常见的单词](https://leetcode-cn.com/problems/most-common-word/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/most-common-word/solution/by-ac_oier-6aqd/) | 简单 | 🤩🤩🤩🤩 |
108108
| [821. 字符的最短距离](https://leetcode-cn.com/problems/shortest-distance-to-a-character/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/shortest-distance-to-a-character/solution/by-ac_oier-5bjs/) | 简单 | 🤩🤩🤩🤩 |
109109
| [824. 山羊拉丁文](https://leetcode-cn.com/problems/goat-latin/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/goat-latin/solution/by-ac_oier-t7hj/) | 简单 | 🤩🤩🤩🤩 |
110+
| [828. 统计子串中的唯一字符](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/solution/by-ac_oier-922k/) | 困难 | 🤩🤩🤩🤩 |
110111
| [846. 一手顺子](https://leetcode-cn.com/problems/hand-of-straights/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/hand-of-straights/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-4hxw/) | 中等 | 🤩🤩🤩 |
111112
| [859. 亲密字符串](https://leetcode-cn.com/problems/buddy-strings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/buddy-strings/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-q056/) | 简单 | 🤩🤩🤩🤩🤩 |
112113
| [867. 转置矩阵](https://leetcode-cn.com/problems/transpose-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/transpose-matrix/solution/yi-you-wei-jin-huo-xu-ni-neng-kan-kan-zh-m53m/) | 简单 | 🤩🤩🤩🤩 |

‎Index/脑筋急转弯.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
| [406. 根据身高重建队列](https://leetcode.cn/problems/queue-reconstruction-by-height/) | [LeetCode 题解链接](https://leetcode.cn/problems/queue-reconstruction-by-height/solution/by-ac_oier-fda2/) | 中等 | 🤩🤩🤩 |
55
| [419. 甲板上的战舰](https://leetcode-cn.com/problems/battleships-in-a-board/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/battleships-in-a-board/solution/gong-shui-san-xie-ji-chong-sao-miao-xian-trmc/) | 中等 | 🤩🤩🤩🤩 |
66
| [423. 从英文中重建数字](https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/solution/gong-shui-san-xie-nao-jin-ji-zhuan-wan-m-vg7a/) | 中等 | 🤩🤩🤩🤩 |
7+
| [667. 优美的排列 II](https://leetcode.cn/problems/beautiful-arrangement-ii/) | [LeetCode 题解链接](https://leetcode.cn/problems/beautiful-arrangement-ii/solution/by-ac_oier-lyns/) | 中等 | 🤩🤩🤩🤩 |
78
| [908. 最小差值 I](https://leetcode.cn/problems/smallest-range-i/) | [LeetCode 题解链接](https://leetcode.cn/problems/smallest-range-i/solution/by-ac_oier-7fh0/) | 简单 | 🤩🤩🤩🤩 |
89
| [2038. 如果相邻两个颜色均相同则删除当前颜色](https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/solution/gong-shui-san-xie-nao-jin-ji-zhuan-wan-y-a8xm/) | 中等 | 🤩🤩🤩🤩🤩 |
910
| [2069. 模拟行走机器人 II](https://leetcode-cn.com/problems/walking-robot-simulation-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/walking-robot-simulation-ii/solution/by-ac_oier-6zib/) | 中等 | 🤩🤩🤩🤩 |
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[667. 优美的排列 II](https://leetcode.cn/problems/beautiful-arrangement-ii/solution/by-ac_oier-lyns/)** ,难度为 **中等**
4+
5+
Tag : 「构造」、「脑筋急转弯」
6+
7+
8+
9+
给你两个整数 `n``k` ,请你构造一个答案列表 `answer`,该列表应当包含从 `1``n``n` 个不同正整数,并同时满足下述条件:
10+
11+
假设该列表是 $answer = [a_1, a_2, a_3, ... , a_n]$ ,那么列表 $[|a_1 - a_2|, |a_2 - a_3|, |a_3 - a_4|, ... , |a_{n-1} - a_n|]$ 中应该有且仅有 k 个不同整数。
12+
13+
返回列表 `answer`。如果存在多种答案,只需返回其中 任意一种 。
14+
15+
示例 1:
16+
```
17+
输入:n = 3, k = 1
18+
19+
输出:[1, 2, 3]
20+
21+
解释:[1, 2, 3] 包含 3 个范围在 1-3 的不同整数,并且 [1, 1] 中有且仅有 1 个不同整数:1
22+
```
23+
示例 2:
24+
```
25+
输入:n = 3, k = 2
26+
27+
输出:[1, 3, 2]
28+
29+
解释:[1, 3, 2] 包含 3 个范围在 1-3 的不同整数,并且 [2, 1] 中有且仅有 2 个不同整数:1 和 2
30+
```
31+
32+
提示:
33+
* 1ドル <= k < n <= 10^4$
34+
35+
---
36+
37+
### 构造
38+
39+
给定范围在 $[1, n]$ 的 $n$ 个数,当「直接升序/降序」排列时,相邻项差值为 1ドル,ドル仅一种;而从首位开始按照「升序」间隔排列,次位开始按照「降序」间隔排列(即排列为 `[1, n, 2, n - 1, 3, ...]`)时,相邻差值会从 $n - 1$ 开始递减至 1ドル,ドル共 $n - 1$ 种。
40+
41+
那么当我们需要构造 $k$ 种序列时,我们可以先通过「直接升序」的方式构造出若干个 1ドル,ドル然后再通过「间隔位分别升降序」的方式构造出从 $k$ 到 1ドル$ 的差值,共 $k$ 个。
42+
43+
显然,我们需要 $k + 1$ 个数来构造出 $k$ 个差值。因此我们可以先从 1ドル$ 开始,使用 $n - (k + 1)$ 个数来直接升序(通过方式一构造出若干个 1ドル$),然后从 $n - k$ 开始间隔升序排列,按照 $n$ 开始间隔降序排列,构造出剩下的序列。
44+
45+
Java 代码:
46+
```Java
47+
class Solution {
48+
public int[] constructArray(int n, int k) {
49+
int[] ans = new int[n];
50+
int t = n - k - 1;
51+
for (int i = 0; i < t; i++) ans[i] = i + 1;
52+
for (int i = t, a = n - k, b = n; i < n; ) {
53+
ans[i++] = a++;
54+
if (i < n) ans[i++] = b--;
55+
}
56+
return ans;
57+
}
58+
}
59+
```
60+
TypeScript 代码:
61+
```TypeScript
62+
function constructArray(n: number, k: number): number[] {
63+
const ans = new Array<number>(n).fill(0)
64+
const t = n - k - 1
65+
for (let i = 0; i < t; i++) ans[i] = i + 1
66+
for (let i = t, a = n - k, b = n; i < n; ) {
67+
ans[i++] = a++
68+
if (i < n) ans[i++] = b--
69+
}
70+
return ans
71+
};
72+
```
73+
* 时间复杂度:$O(n)$
74+
* 空间复杂度:$O(n)$
75+
76+
---
77+
78+
### 最后
79+
80+
这是我们「刷穿 LeetCode」系列文章的第 `No.667` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
81+
82+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
83+
84+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
85+
86+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
87+
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[828. 统计子串中的唯一字符](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/solution/by-ac_oier-922k/)** ,难度为 **困难**
4+
5+
Tag : 「模拟」、「数学」
6+
7+
8+
9+
我们定义了一个函数 `countUniqueChars(s)` 来统计字符串 `s` 中的唯一字符,并返回唯一字符的个数。
10+
11+
例如:`s = "LEETCODE"` ,则其中 `"L"`, `"T"`,`"C"`,`"O"`,`"D"` 都是唯一字符,因为它们只出现一次,所以 `countUniqueChars(s) = 5`
12+
13+
本题将会给你一个字符串 `s` ,我们需要返回 `countUniqueChars(t)` 的总和,其中 `t``s` 的子字符串。输入用例保证返回值为 `32` 位整数。
14+
15+
注意,某些子字符串可能是重复的,但你统计时也必须算上这些重复的子字符串(也就是说,你必须统计 `s` 的所有子字符串中的唯一字符)。
16+
17+
示例 1:
18+
```
19+
输入: s = "ABC"
20+
21+
输出: 10
22+
23+
解释: 所有可能的子串为:"A","B","C","AB","BC" 和 "ABC"。
24+
其中,每一个子串都由独特字符构成。
25+
所以其长度总和为:1 +たす 1 +たす 1 +たす 2 +たす 2 +たす 3 = 10
26+
```
27+
示例 2:
28+
```
29+
输入: s = "ABA"
30+
31+
输出: 8
32+
33+
解释: 除了 countUniqueChars("ABA") = 1 之外,其余与示例 1 相同。
34+
```
35+
示例 3:
36+
```
37+
输入:s = "LEETCODE"
38+
39+
输出:92
40+
```
41+
42+
提示:
43+
* 1ドル <= s.length <= 10^5$
44+
* `s` 只包含大写英文字符
45+
46+
---
47+
48+
### 模拟 + 乘法原理
49+
50+
这道题和 [907. 子数组的最小值之和](https://mp.weixin.qq.com/s?__biz=MzU4NDE3MTEyMA==&mid=2247493485&idx=1&sn=c6d6ac25c171e2617c5a95173aa58825) 如出一辙,只不过无须考虑运用「单调栈」。
51+
52+
原问题为求所有子数组的唯一字符数量和,其可等价为求每个 $s[i]$ 对答案的贡献,即每个 $s[i]$ 可作为多少个子数组的唯一元素。
53+
54+
假定我们能预处理出两数组 `l``r` 分别代表 $s[i]$ 作为子数组唯一字符时,其所能到达的最远两端:
55+
* `l[i] = a` 代表下标 $a$ 为 $s[i]$ 能够作为子数组唯一字符时的最远左边界,即为 $s[i]$ 左边第一个与 $s[i]$ 值相同的位置(若不存在,则为 $a = -1$)
56+
* `r[i] = b` 代表跳表 $b$ 为 $s[i]$ 能够作为子数组唯一字符时的最远右边界,即为 $s[i]$ 右边第一个与 $s[i]$ 值相同的位置(若不存在,则为 $b = n$)
57+
58+
子数组左端点个数为 $(i - a)$ 个,右端点个数为 $(b - i)$ 个,根据乘法原理可知,子数组个数为两者乘积。
59+
60+
预处理 `l``r` 只需要使用遍历计数即可。
61+
62+
Java 代码:
63+
```Java
64+
class Solution {
65+
public int uniqueLetterString(String s) {
66+
char[] cs = s.toCharArray();
67+
int n = cs.length, ans = 0;
68+
int[] l = new int[n], r = new int[n];
69+
int[] idx = new int[26];
70+
Arrays.fill(idx, -1);
71+
for (int i = 0; i < n; i++) {
72+
int u = cs[i] - 'A';
73+
l[i] = idx[u];
74+
idx[u] = i;
75+
}
76+
Arrays.fill(idx, n);
77+
for (int i = n - 1; i >= 0; i--) {
78+
int u = cs[i] - 'A';
79+
r[i] = idx[u];
80+
idx[u] = i;
81+
}
82+
for (int i = 0; i < n; i++) ans += (i - l[i]) * (r[i] - i);
83+
return ans;
84+
}
85+
}
86+
```
87+
Typescript 代码:
88+
```Typescript
89+
function uniqueLetterString(s: string): number {
90+
let n = s.length, ans = 0
91+
const l = new Array<number>(n), r = new Array<number>(n)
92+
const idx = new Array<number>(26).fill(-1)
93+
for (let i = 0; i < n; i++) {
94+
const u = s.charCodeAt(i) - 65
95+
l[i] = idx[u]
96+
idx[u] = i
97+
}
98+
idx.fill(n)
99+
for (let i = n - 1; i >= 0; i--) {
100+
const u = s.charCodeAt(i) - 65
101+
r[i] = idx[u]
102+
idx[u] = i
103+
}
104+
for (let i = 0; i < n; i++) ans += (i - l[i]) * (r[i] - i)
105+
return ans
106+
};
107+
```
108+
* 时间复杂度:$O(n)$
109+
* 空间复杂度:$O(n)$
110+
111+
---
112+
113+
### 最后
114+
115+
这是我们「刷穿 LeetCode」系列文章的第 `No.828` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
116+
117+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
118+
119+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
120+
121+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
122+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /