Skip to content

Commit 13c1172

Browse files
authored
update: 添加问题“1622.奇妙序列”的代码和题解+docs(swap) (#1444)
* 1415: AC.cpp.dfs (#1441) - AC,DFS+引用,100.00%,96.10% * 1415: AC.cpp.dfs (#1441) - AC,DFS+引用+直接操作ans,100.00%,98.29% * word: en 2026.3.15 (#1443) * 1662: CE.cpp (#1443) - Line 29: Char 19: runtime error: signed integer overflow: -4702111234474983746 * -4702111234474983746 cannot be represented in type 'll' (aka 'long long') (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:34:19 * 1662: (easier to read).cpp (#1443) * 1662: dbg.cpp (#1443) * 1662: RE.cpp (#1443) * 1662: WA.cpp (#1443) * 1662: AC.cpp (#1443) - AC,95.45%,92.61% mul的初始值应该为1而非为0 * 1662: WA.py (#1443) * 1662: AC.py (#1443) - AC,94.03%,52.24% * update: 添加问题“1622.奇妙序列”的代码和题解 (#1444) Signed-off-by: LetMeFly666 <Tisfy@qq.com> * feat: new line at the end Apply suggestions from code review Co-authored-by: LetMeFly <Tisfy@qq.com> * typo: 记得 * docs: linux swap mem --------- Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent 6a1d4b6 commit 13c1172

8 files changed

Lines changed: 460 additions & 19 deletions

Codes/1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n_DFS.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2026-03-14 22:56:17
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2026-03-14 23:33:32
5+
* @LastEditTime: 2026-03-15 09:48:29
66
*/
77
#ifdef _DEBUG
88
#include "_[1,2]toVector.h"
@@ -15,31 +15,32 @@ class Solution {
1515
char can[3] = {'a', 'b', 'c'};
1616

1717
// dfs and return if can stop
18-
bool dfs(string now) {
19-
if (now.size() == n) {
18+
bool dfs() {
19+
if (ans.size() == n) {
2020
k--;
2121
if (!k) {
22-
ans = now;
2322
return true;
2423
}
2524
return false;
2625
}
2726

28-
char last = now.empty() ? '0' : now.back();
27+
char last = ans.empty() ? '0' : ans.back();
2928
for (int i = 0; i < 3; i++) {
3029
if (can[i] == last) {
3130
continue;
3231
}
33-
if (dfs(now + can[i])) {
32+
ans += can[i];
33+
if (dfs()) {
3434
return true;
3535
}
36+
ans.pop_back();
3637
}
37-
return false;
38+
return false;
3839
}
3940
public:
4041
string getHappyString(int n, int k) {
4142
this->n = n, this->k = k;
42-
dfs("");
43+
dfs();
4344
return ans;
4445
}
4546
};

Codes/1622-fancy-sequence.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-03-15 10:21:03
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-03-15 11:19:41
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
const ll MOD = 1e9 + 7;
13+
14+
15+
class Num {
16+
private:
17+
ll val;
18+
19+
ll power(ll a, ll n) const {
20+
ll ans = 1;
21+
for (; n; n >>= 1) {
22+
if (n & 1) {
23+
ans = ans * a % MOD;
24+
}
25+
a = a * a % MOD;
26+
}
27+
return ans;
28+
}
29+
public:
30+
Num(): val(0) {} // 记得初始化val
31+
Num(ll n) : val(n % MOD) {}
32+
Num(int n) : val(n % MOD) {}
33+
Num operator+(const ll& b) const { return Num((val + b) % MOD); }
34+
Num operator-(const ll& b) const { return Num((val - b + MOD) % MOD); }
35+
Num operator*(const ll& b) const { return Num(val * b % MOD); }
36+
Num operator/(const ll& b) const { return Num(val * power(b, MOD - 2) % MOD); }
37+
Num operator+(const Num& b) const { return Num((val + b.val) % MOD); }
38+
Num operator-(const Num& b) const { return Num((val - b.val + MOD) % MOD); }
39+
Num operator*(const Num& b) const { return Num(val * b.val % MOD); }
40+
Num operator/(const Num& b) const { return Num(val * power(b.val, MOD - 2) % MOD); }
41+
Num& operator+=(const ll& b) { return *this = *this + b; }
42+
Num& operator-=(const ll& b) { return *this = *this - b; }
43+
Num& operator*=(const ll& b) { return *this = *this * b; }
44+
Num& operator/=(const ll& b) { return *this = *this / b; }
45+
operator int() const { return static_cast<int>(val); }
46+
};
47+
48+
class Fancy {
49+
private:
50+
vector<Num> vals;
51+
Num add;
52+
Num mul;
53+
public:
54+
Fancy() {
55+
mul = Num(1);
56+
}
57+
58+
void append(int val) {
59+
vals.push_back((Num(val) - add) / mul);
60+
}
61+
62+
void addAll(int inc) {
63+
add += inc;
64+
}
65+
66+
void multAll(int m) {
67+
add *= m;
68+
mul *= m;
69+
}
70+
71+
int getIndex(int idx) {
72+
return idx >= vals.size() ? -1 : (int)(vals[idx] * mul + add);
73+
}
74+
};
75+
76+
/**
77+
* Your Fancy object will be instantiated and called as such:
78+
* Fancy* obj = new Fancy();
79+
* obj->append(val);
80+
* obj->addAll(inc);
81+
* obj->multAll(m);
82+
* int param_4 = obj->getIndex(idx);
83+
*/
84+
#ifdef _DEBUG
85+
/*
86+
["Fancy","append","addAll","append","multAll","getIndex","addAll","append","multAll","getIndex","getIndex","getIndex"]
87+
[[],[2],[3],[7],[2],[0],[3],[10],[2],[0],[1],[2]]
88+
89+
*/
90+
int main() {
91+
string a, b;
92+
while (cin >> a >> b) {
93+
vector<string> va = stringToVectorString(a);
94+
vector<vector<int>> vb = stringToVectorVector(b);
95+
if (va.size() != vb.size()) {
96+
cout << "ERROR length" << endl;
97+
continue;
98+
}
99+
Fancy fancy;
100+
for (int i = 0; i < va.size(); i++) {
101+
string s = va[i];
102+
if (!s.empty() && s[0] == '"') {
103+
s.erase(s.begin());
104+
}
105+
if (!s.empty() && s.back() == '"') {
106+
s.erase(prev(s.end()));
107+
}
108+
int val = vb[i].empty() ? 0 : vb[i][0];
109+
if (s == "Fancy") {
110+
fancy = Fancy();
111+
} else if (s == "append") {
112+
fancy.append(val);
113+
} else if (s == "addAll") {
114+
fancy.addAll(val);
115+
} else if (s == "multAll") {
116+
fancy.multAll(val);
117+
} else if (s == "getIndex") {
118+
cout << fancy.getIndex(val) << endl;
119+
} else {
120+
cout << "ERROR" << endl;
121+
}
122+
}
123+
}
124+
return 0;
125+
}
126+
#endif

Codes/1622-fancy-sequence.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2026-03-15 10:21:03
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2026-03-15 11:25:58
6+
'''
7+
MOD = 1000000007
8+
9+
class Fancy:
10+
def __init__(self):
11+
self.add = 0
12+
self.mul = 1
13+
self.vals = []
14+
15+
def append(self, val: int) -> None:
16+
self.vals.append((val - self.add + MOD) * pow(self.mul, -1, MOD) % MOD)
17+
18+
def addAll(self, inc: int) -> None:
19+
self.add = (self.add + inc) % MOD
20+
21+
def multAll(self, m: int) -> None:
22+
self.add = self.add * m % MOD
23+
self.mul = self.mul * m % MOD
24+
25+
def getIndex(self, idx: int) -> int:
26+
if idx >= len(self.vals):
27+
return -1
28+
return (self.vals[idx] * self.mul + self.add) % MOD
29+
30+
31+
# Your Fancy object will be instantiated and called as such:
32+
# obj = Fancy()
33+
# obj.append(val)
34+
# obj.addAll(inc)
35+
# obj.multAll(m)
36+
# param_4 = obj.getIndex(idx)

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2022-05-19 18:48:53
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2026-02-21 11:12:11
5+
* @LastEditTime: 2026-03-15 13:03:28
66
-->
77
# LetLeet Blog
88

@@ -95,6 +95,7 @@
9595
|力扣2022年1024卡牌活动,程序员怎么判断自己的卡牌能否组成1024?那就愉快地编程实现吧!|<a href="https://blog.letmefly.xyz/2022/10/19/Other-LeetCode1024-2022/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/127413787">CSDN博客</a>|
9696
|Linux - 内存相关 - 减小Mysql的内存占用 or 查看内存使用情况|<a href="https://blog.letmefly.xyz/2023/02/20/Other-Linux-MysqlMemReduce/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/129120029">CSDN博客</a>|
9797
|Linux - SSH - SSH免密登录(假设已生成过rsa key pair)|<a href="https://blog.letmefly.xyz/2023/04/22/Other-Linux-SSHLoginWithoutPassword/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/130302379">CSDN博客</a>|
98+
|Linux Swap 文件完全指南:从创建到调优 - Written By AI(Claude-Opus4.6)|<a href="https://blog.letmefly.xyz/2026/03/15/Other-Linux-SwapMemSetting/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/159080576">CSDN博客</a>|
9899
|MacOS - 记录MacOS发烫的好几天 - 幕后黑手竟然是|<a href="https://blog.letmefly.xyz/2025/09/01/Other-MacOS-Logging_several_days_of_macOS_overheating/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/151087541">CSDN博客</a>|
99100
|MacOS - Clang使用bits/stdc++.h - 非官方(竞赛用) - 通用方法|<a href="https://blog.letmefly.xyz/2025/09/27/Other-MacOS-ClangUsingBits_stdcpp_h/">本平台博客</a>|<a href="https://letmefly.blog.csdn.net/article/details/152164818">CSDN博客</a>|
100101
|图论笔记 - 极简极入门级|<a href="https://blog.letmefly.xyz/2023/10/27/Other-Math-GraphTheory-Notes/">本平台博客</a>|无|
@@ -627,6 +628,7 @@
627628
|1616.分割两个字符串得到回文串|中等|<a href="https://leetcode.cn/problems/split-two-strings-to-make-palindrome/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/18/LeetCode%201616.%E5%88%86%E5%89%B2%E4%B8%A4%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%BE%97%E5%88%B0%E5%9B%9E%E6%96%87%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129635845" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/split-two-strings-to-make-palindrome/solutions/2176141/letmefly-1616fen-ge-liang-ge-zi-fu-chuan-niyo/" target="_blank">LeetCode题解</a>|
628629
|1619.删除某些元素后的数组均值|简单|<a href="https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/09/14/LeetCode%201619.%E5%88%A0%E9%99%A4%E6%9F%90%E4%BA%9B%E5%85%83%E7%B4%A0%E5%90%8E%E7%9A%84%E6%95%B0%E7%BB%84%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126850931" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/mean-of-array-after-removing-some-elements/solution/letmefly-1619shan-chu-mou-xie-yuan-su-ho-ei4e/" target="_blank">LeetCode题解</a>|
629630
|1620.网络信号最好的坐标|中等|<a href="https://leetcode.cn/problems/coordinate-with-maximum-network-quality/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/11/02/LeetCode%201620.%E7%BD%91%E7%BB%9C%E4%BF%A1%E5%8F%B7%E6%9C%80%E5%A5%BD%E7%9A%84%E5%9D%90%E6%A0%87/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127646389" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/coordinate-with-maximum-network-quality/solution/letmefly-1620wang-luo-xin-hao-zui-hao-de-43gg/" target="_blank">LeetCode题解</a>|
631+
|1622.奇妙序列|困难|<a href="https://leetcode.cn/problems/fancy-sequence/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/03/15/LeetCode%201622.%E5%A5%87%E5%A6%99%E5%BA%8F%E5%88%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/159079979" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/fancy-sequence/solutions/3925847/letmefly-1622qi-miao-xu-lie-lan-geng-xin-4bs2/" target="_blank">LeetCode题解</a>|
630632
|1624.两个相同字符之间的最长子字符串|简单|<a href="https://leetcode.cn/problems/largest-substring-between-two-equal-characters/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/09/17/LeetCode%201624.%E4%B8%A4%E4%B8%AA%E7%9B%B8%E5%90%8C%E5%AD%97%E7%AC%A6%E4%B9%8B%E9%97%B4%E7%9A%84%E6%9C%80%E9%95%BF%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126900794" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/largest-substring-between-two-equal-characters/solution/letmefly-1624liang-ge-xiang-tong-zi-fu-z-a6u9/" target="_blank">LeetCode题解</a>|
631633
|1625.执行操作后字典序最小的字符串|中等|<a href="https://leetcode.cn/problems/lexicographically-smallest-string-after-applying-operations/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/19/LeetCode%201625.%E6%89%A7%E8%A1%8C%E6%93%8D%E4%BD%9C%E5%90%8E%E5%AD%97%E5%85%B8%E5%BA%8F%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129651164" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/lexicographically-smallest-string-after-applying-operations/solutions/2178248/letmefly-1625zhi-xing-cao-zuo-hou-zi-dia-pcfz/" target="_blank">LeetCode题解</a>|
632634
|1630.等差子数组|中等|<a href="https://leetcode.cn/problems/arithmetic-subarrays/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/23/LeetCode%201630.%E7%AD%89%E5%B7%AE%E5%AD%90%E6%95%B0%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129736480" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/arithmetic-subarrays/solutions/2186770/letmefly-1630deng-chai-zi-shu-zu-by-tisf-0ecz/" target="_blank">LeetCode题解</a>|

Solutions/LeetCode 1415.长度为n的开心字符串中字典序第k小的字符串.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: 1415.长度为 n 的开心字符串中字典序第 k 小的字符串:DFS构造 / 数学O(1)
2+
title: 1415.长度为 n 的开心字符串中字典序第 k 小的字符串:DFS构造 / 数学O(n)
33
date: 2026-03-14 23:31:04
44
tags: [题解, LeetCode, 中等, 字符串, 回溯, 深度优先搜索, DFS, 模拟, 数学, 构造]
55
categories: [题解, LeetCode]
@@ -97,31 +97,32 @@ private:
9797
char can[3] = {'a', 'b', 'c'};
9898

9999
// dfs and return if can stop
100-
bool dfs(string now) {
101-
if (now.size() == n) {
100+
bool dfs() {
101+
if (ans.size() == n) {
102102
k--;
103103
if (!k) {
104-
ans = now;
105104
return true;
106105
}
107106
return false;
108107
}
109108

110-
char last = now.empty() ? '0' : now.back();
109+
char last = ans.empty() ? '0' : ans.back();
111110
for (int i = 0; i < 3; i++) {
112111
if (can[i] == last) {
113112
continue;
114113
}
115-
if (dfs(now + can[i])) {
114+
ans += can[i];
115+
if (dfs()) {
116116
return true;
117117
}
118+
ans.pop_back();
118119
}
119-
return false;
120+
return false;
120121
}
121122
public:
122123
string getHappyString(int n, int k) {
123124
this->n = n, this->k = k;
124-
dfs("");
125+
dfs();
125126
return ans;
126127
}
127128
};

0 commit comments

Comments
 (0)