Skip to content

Commit 4ecbd84

Browse files
committed
feat: 20260226 check in
Made-with: Cursor
1 parent aee5cd9 commit 4ecbd84

3 files changed

Lines changed: 147 additions & 4 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [1404. 将二进制表示减到 1 的步骤数](https://leetcode.cn/problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one/description/)
2+
3+
> **日期**:2026-02-26
4+
> **所用时间**:10min
5+
> **知识点**:模拟、位运算
6+
7+
## 1. 题目描述
8+
9+
给你一个以二进制形式表示的数字 `s`。请你返回将它变成 **1** 所需要的步数。
10+
11+
规则如下:
12+
13+
- 如果当前数字是**偶数**,则将其除以 2。
14+
- 如果当前数字是**奇数**,则将其加 1。
15+
16+
题目保证 `s` 对应的十进制数一定可以经过若干步变成 1。
17+
18+
**示例 1:**
19+
20+
- **输入**:s = "1101"
21+
- **输出**:6
22+
- **解释**:二进制 "1101" 对应十进制 13。13 是奇数,加 1 得 14;14 是偶数,除以 2 得 7;7 是奇数,加 1 得 8;8 是偶数,除以 2 得 4;4 除以 2 得 2;2 除以 2 得 1。共 6 步。
23+
24+
**示例 2:**
25+
26+
- **输入**:s = "10"
27+
- **输出**:1
28+
- **解释**:二进制 "10" 对应 2,2 除以 2 得 1,共 1 步。
29+
30+
**示例 3:**
31+
32+
- **输入**:s = "1"
33+
- **输出**:0
34+
- **解释**:已经是 1,不需要操作。
35+
36+
**提示:**
37+
38+
- `1 <= s.length <= 500`
39+
- `s` 由字符 `'0'``'1'` 组成
40+
- `s[0] == '1'`(即二进制表示至少有一位且最高位为 1)
41+
42+
## 2. 模拟
43+
44+
直接模拟:先将二进制字符串转为整数,然后反复执行「若为偶数则除以 2,否则加 1」,并统计步数直到变为 1。偶数等价于最低位为 0,奇数等价于最低位为 1,因此用位运算实现除以 2(右移)和加 1 即可。
45+
46+
复杂度分析:
47+
48+
- 时间复杂度:$O(n + \log v)$,其中 $n$ 为字符串长度,$v$ 为数值;转整数 $O(n)$,模拟步数最多 $O(\log v)$(每次至少减半或加一后变偶数再减半)。
49+
- 空间复杂度:$O(1)$(若不计转整数后的大整数存储)。
50+
51+
**Python3**
52+
53+
```python
54+
class Solution:
55+
def numSteps(self, s: str) -> int:
56+
ans = 0
57+
s = int(s, 2)
58+
59+
while s > 1:
60+
if s % 2 == 0:
61+
s >>= 1
62+
else:
63+
s += 1
64+
ans += 1
65+
return ans
66+
```

leetcode/8-119经典题变种挑战/挑战 2:数组/LCR 010. 和为 K 的子数组.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# [LCR 010. 和为 K 的子数组](https://leetcode.cn/problems/QTMn0o/description/)
22

3-
> **作者**:弘树
43
> **日期**:2024-10-11
54
> **所用时间**:11min
65
@@ -54,10 +53,12 @@ public:
5453
class Solution:
5554
def subarraySum(self, nums: List[int], k: int) -> int:
5655
ans = s = 0
57-
mp = {0: 1}
56+
pos = defaultdict(int)
57+
pos[0] = 1
58+
5859
for x in nums:
5960
s += x
60-
ans += mp.get(s - k, 0)
61-
mp[s] = mp.get(s, 0) + 1
61+
ans += pos[s - k]
62+
pos[s] += 1
6263
return ans
6364
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# [3628. 插入一个字母的最大子序列数](https://leetcode.cn/problems/maximum-subsequence-score-after-applying-operations/description/)
2+
3+
> **日期**:2026-02-26
4+
> **所用时间**:20min
5+
> **知识点**:动态规划
6+
7+
## 1. 题目描述
8+
9+
给定一个仅由字符 `'L'``'C'``'T'` 组成的字符串 `s`。你可以在 `s`**任意位置**(包括首尾)**插入**恰好一个字符(也只能是 `'L'``'C'``'T'`)。
10+
11+
插入后,得到一个新字符串。请计算新字符串中等于 **"LCT"****不同子序列**的个数,并返回在所有插入方案中,该个数的**最大值**
12+
13+
**说明**:子序列不要求连续,只要相对顺序与 "LCT" 相同即算一种。两个子序列若取的下标集合不同则视为不同。
14+
15+
**示例 1:**
16+
17+
- **输入**:s = "LCT"
18+
- **输出**:2
19+
- **解释**:原串已有 1 个 "LCT"。插入一个字符后,例如在 'L' 与 'C' 之间插入 'C' 得到 "L C C T",可形成更多 "LCT" 子序列,最大为 2(或其他方案,以题目为准)。
20+
21+
**示例 2:**
22+
23+
- **输入**:s = "LTT"
24+
- **输出**:1
25+
- **解释**:原串中 "LCT" 子序列数为 0。插入 'C' 在合适位置后可得到 1 个 "LCT"。
26+
27+
**提示:**
28+
29+
- `1 <= s.length <= 10^5`
30+
- `s` 仅包含 `'L'``'C'``'T'`
31+
32+
## 2. 动态规划
33+
34+
原串的 "LCT" 不同子序列数用经典 DP:`f[i][j]` 表示用 `s[0..i-1]` 形成 `t[0..j-1]`(其中 `t = "LCT"`)的方案数,转移为「不选 s[i]」或「选 s[i] 匹配 t[j]」(当 `s[i]==t[j]` 时)。则原串答案为 `numDistinct(s, "LCT")`
35+
36+
插入一个字符后,最大值 = 原串 "LCT" 数 + 插入带来的最大增益:
37+
38+
1. **插入 'C'**:在某个位置插入 'C',该位置左侧的每个 'L' 与右侧的每个 'T' 都能与这个 'C' 组成新的 "LCT",增益为左侧 L 的个数 × 右侧 T 的个数。枚举插入位置,取 `max(cnt_l * cnt_t)` 即为插入 'C' 的最大增益。
39+
2. **插入 'L'**:新 'L' 可与原串中的 "CT" 子序列组成 "LCT",增益最多为原串中 "CT" 的不同子序列数,即 `numDistinct(s, "CT")`
40+
3. **插入 'T'**:新 'T' 可与原串中的 "LC" 子序列组成 "LCT",增益最多为 `numDistinct(s, "LC")`
41+
42+
因此答案为:`numDistinct(s, "LCT") + max( max_pos(cnt_l * cnt_t), numDistinct(s, "LC"), numDistinct(s, "CT") )`
43+
44+
复杂度分析:
45+
46+
- 时间复杂度:$O(n \cdot |t|)$,其中 $n = |s|$,$|t| = 3$,即 $O(n)$;扫描求 `cnt_l``cnt_t` 为 $O(n)$。
47+
- 空间复杂度:$O(n \cdot |t|)$,即 $O(n)$(可滚动数组优化为 $O(|t|)$)。
48+
49+
**Python3**
50+
51+
```python
52+
class Solution:
53+
def numOfSubsequences(self, s: str) -> int:
54+
def numDistinct(s: str, t: str) -> int:
55+
n, m = len(s), len(t)
56+
f = [[1] + [0] * m for _ in range(n + 1)]
57+
58+
for i, c1 in enumerate(s):
59+
for j, c2 in enumerate(t):
60+
if c1 != c2:
61+
f[i + 1][j + 1] = f[i][j + 1]
62+
else:
63+
f[i + 1][j + 1] = f[i][j + 1] + f[i][j]
64+
return f[n][m]
65+
66+
cnt_t = s.count('T')
67+
cnt_l = 0
68+
res = 0
69+
for c in s:
70+
if c == 'T':
71+
cnt_t -= 1
72+
if c == 'L':
73+
cnt_l += 1
74+
res = max(res, cnt_l * cnt_t)
75+
return numDistinct(s, "LCT") + max(res, numDistinct(s, "LC"), numDistinct(s, "CT"))
76+
```

0 commit comments

Comments
 (0)