Skip to content

Commit c5d81c8

Browse files
committed
✨feat: add 1818
1 parent 60bf91c commit c5d81c8

File tree

3 files changed

+94
-11
lines changed

3 files changed

+94
-11
lines changed

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
| [1331. 数组序号转换](https://leetcode.cn/problems/rank-transform-of-an-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/rank-transform-of-an-array/solution/by-ac_oier-j70n/) | 简单 | 🤩🤩🤩🤩 |
138138
| [1332. 删除回文子序列](https://leetcode-cn.com/problems/remove-palindromic-subsequences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-palindromic-subsequences/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-0zwn/) | 中等 | 🤩🤩🤩🤩 |
139139
| [1342. 将数字变成 0 的操作次数](https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/solution/gong-shui-san-xie-note-bie-pian-yi-ti-sh-85fb/) | 简单 | 🤩🤩🤩🤩 |
140+
| [1374. 生成每种字符都是奇数个的字符串](https://leetcode.cn/problems/generate-a-string-with-characters-that-have-odd-counts/) | [LeetCode 题解链接](https://leetcode.cn/problems/generate-a-string-with-characters-that-have-odd-counts/solution/by-ac_oier-i74n/) | 简单 | 🤩🤩 |
140141
| [1380. 矩阵中的幸运数](https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-9xwg/) | 简单 | 🤩🤩🤩 |
141142
| [1436. 旅行终点站](https://leetcode-cn.com/problems/destination-city/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/destination-city/solution/gong-shui-san-xie-jian-dan-fang-jia-mo-n-y47c/) | 简单 | 🤩🤩🤩🤩🤩 |
142143
| [1446. 连续字符](https://leetcode-cn.com/problems/consecutive-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/consecutive-characters/solution/gong-shui-san-xie-jian-dan-shuang-zhi-zh-xtv6/) | 简单 | 🤩🤩🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1374. 生成每种字符都是奇数个的字符串](https://leetcode.cn/problems/generate-a-string-with-characters-that-have-odd-counts/solution/by-ac_oier-i74n/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给你一个整数 `n`,请你返回一个含 `n` 个字符的字符串,其中每种字符在该字符串中都恰好出现 奇数次 。
10+
11+
返回的字符串必须只含小写英文字母。如果存在多个满足题目要求的字符串,则返回其中任意一个即可。
12+
13+
示例 1:
14+
```
15+
输入:n = 4
16+
17+
输出:"pppz"
18+
19+
解释:"pppz" 是一个满足题目要求的字符串,因为 'p' 出现 3 次,且 'z' 出现 1 次。当然,还有很多其他字符串也满足题目要求,比如:"ohhh" 和 "love"。
20+
```
21+
示例 2:
22+
```
23+
输入:n = 2
24+
25+
输出:"xy"
26+
27+
解释:"xy" 是一个满足题目要求的字符串,因为 'x' 和 'y' 各出现 1 次。当然,还有很多其他字符串也满足题目要求,比如:"ag" 和 "ur"。
28+
```
29+
示例 3:
30+
```
31+
输入:n = 7
32+
33+
输出:"holasss"
34+
```
35+
36+
提示:
37+
* $1 <= n <= 500$
38+
39+
---
40+
41+
### 模拟
42+
43+
题目仅规定所用到的字符出现次数均为奇数,并无规定单个字符的最大使用次数。
44+
45+
因此直接根据 $n$ 的奇偶性来做即可,若 $n$ 为奇数,则构造出由 $n$ 个 `b` 拼接的字符串;若 $n$ 为偶数,则构造出由 $1$ 个 `a` 和 $n - 1$ 个 `b` 的拼接字符串。
46+
47+
Java 代码:
48+
```Java
49+
class Solution {
50+
public String generateTheString(int n) {
51+
StringBuilder sb = new StringBuilder();
52+
if (n % 2 == 0 && --n >= 0) sb.append('a');
53+
while (n-- > 0) sb.append('b');
54+
return sb.toString();
55+
}
56+
}
57+
```
58+
TypeScript 代码:
59+
```TypeScript
60+
function generateTheString(n: number): string {
61+
let ans = ""
62+
if (n % 2 == 0 && --n >= 0) ans += "a"
63+
while (n-- > 0) ans += "b"
64+
return ans
65+
};
66+
```
67+
* 时间复杂度:$O(n)$
68+
* 空间复杂度:$O(n)$
69+
70+
---
71+
72+
### 最后
73+
74+
这是我们「刷穿 LeetCode」系列文章的第 `No.1374` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
75+
76+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
77+
78+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
79+
80+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
81+

LeetCode/1811-1820/1818. 绝对差值和(中等).md

+12-11
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ Tag : 「二分」
66

77

88

9-
给你两个正整数数组 nums1 和 nums2 ,数组的长度都是 n
9+
给你两个正整数数组 `nums1``nums2` ,数组的长度都是 `n`
1010

11-
数组 nums1 和 nums2 的 绝对差值和 定义为所有 |nums1[i] - nums2[i]|(0 <= i < n)的 总和(下标从 0 开始)。
11+
数组 `nums1``nums2`**绝对差值和** 定义为所有 `|nums1[i] - nums2[i]|(0 <= i < n)`的 总和(下标从 $0$ 开始)。
1212

13-
你可以选用 nums1 中的 任意一个 元素来替换 nums1 中的 至多 一个元素,以 最小化 绝对差值和。
13+
你可以选用 `nums1` 中的 **任意一个** 元素来替换 `nums1` 中的 **至多** 一个元素,以 **最小化** 绝对差值和。
1414

15-
在替换数组 nums1 中最多一个元素 之后 ,返回最小绝对差值和。因为答案可能很大,所以需要对 $10^9 + 7$ 取余 后返回。
15+
在替换数组 `nums1` 中最多一个元素 之后 ,返回最小绝对差值和。因为答案可能很大,所以需要对 $10^9 + 7$ 取余 后返回。
1616

17-
|x| 定义为:
18-
* 如果 x >= 0 ,值为 x ,或者
19-
* 如果 x <= 0 ,值为 -x
17+
`|x|` 定义为:
18+
19+
* 如果 $x >= 0$,值为 `x` ,或者
20+
* 如果 $x <= 0$,值为 `-x`
2021

2122

2223

@@ -51,10 +52,10 @@ Tag : 「二分」
5152
```
5253

5354
提示:
54-
* n == nums1.length
55-
* n == nums2.length
56-
* 1 <= n <= $10^5$
57-
* 1 <= nums1[i], nums2[i] <= $10^5$
55+
* $n == nums1.length$
56+
* $n == nums2.length$
57+
* $1 <= n <= 10^5$
58+
* $1 <= nums1[i], nums2[i] <= $10^5$
5859

5960
---
6061

0 commit comments

Comments
 (0)