Skip to content

Commit c0ca910

Browse files
committed
feat: 新增统计特殊字母数量的算法实现及相关文档
1 parent 008069c commit c0ca910

6 files changed

Lines changed: 235 additions & 1 deletion
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @Author: tkzzzzzz6
3+
* @Date: 2026-05-26 22:11:02
4+
* @LastEditors: tkzzzzzz6
5+
* @LastEditTime: 2026-05-26 22:52:41
6+
*/
7+
#include <cmath>
8+
#include <cstdio>
9+
#include <iomanip>
10+
#include <iostream>
11+
12+
using namespace std;
13+
const int N = 1e5 + 10;
14+
double op1[N], op2[N];
15+
16+
int main() {
17+
int n, m;
18+
scanf("%d%d", &n, &m);
19+
20+
op1[0] = 1.0, op2[0] = 0.0;
21+
for (int i = 1; i <= n; ++i) {
22+
int type;
23+
double x;
24+
scanf("%d%lf", &type, &x);
25+
26+
if (type == 1) {
27+
op1[i] = op1[i - 1] * x;
28+
op2[i] = op2[i - 1];
29+
} else {
30+
op1[i] = op1[i - 1];
31+
op2[i] = op2[i - 1] + x;
32+
}
33+
}
34+
while (m--) {
35+
int i, j;
36+
double x, y;
37+
scanf("%d%d%lf%lf", &i, &j, &x, &y);
38+
double k = op1[j] / op1[i - 1];
39+
40+
x = k * x, y = k * y;
41+
double t = op2[j] - op2[i - 1]; // 这里的t采用的弧度制,不用转化
42+
43+
// 注意这里计算ty的时候要用之前的x不能用tx,所以要再声明变量
44+
double tx = x * cos(t) - y * sin(t);
45+
double ty = x * sin(t) + y * cos(t);
46+
47+
printf("%.3lf %.3lf\n", tx, ty);
48+
}
49+
50+
return 0;
51+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @Author: tkzzzzzz6
3+
* @Date: 2026-05-26 21:45:57
4+
* @LastEditors: tkzzzzzz6
5+
* @LastEditTime: 2026-05-26 21:53:28
6+
*/
7+
#include <iostream>
8+
#include <vector>
9+
10+
using namespace std;
11+
12+
int main() {
13+
ios::sync_with_stdio(false);
14+
cin.tie(nullptr);
15+
16+
int n, m, l;
17+
cin >> n >> m >> l;
18+
vector<int> h(l, 0);
19+
20+
for (int i = 0; i < n; ++i)
21+
for (int j = 0; j < m; ++j) {
22+
int x;
23+
cin >> x;
24+
h[x]++;
25+
}
26+
27+
for (int i = 0; i < l; ++i) {
28+
if (i) cout << ' ';
29+
cout << h[i];
30+
}
31+
32+
cout << '\n';
33+
34+
return 0;
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: tkzzzzzz6
3+
* @Date: 2026-05-26 22:00:56
4+
* @LastEditors: tkzzzzzz6
5+
* @LastEditTime: 2026-05-26 22:05:24
6+
*/
7+
#include <iostream>
8+
9+
using namespace std;
10+
11+
int main() {
12+
int n, m;
13+
cin >> n >> m;
14+
15+
int dx = 0, dy = 0;
16+
while (n--) {
17+
int x, y;
18+
cin >> x >> y;
19+
dx += x, dy += y;
20+
}
21+
22+
while (m--) {
23+
int x, y;
24+
cin >> x >> y;
25+
cout << x + dx << ' ' << y + dy << '\n';
26+
}
27+
return 0;
28+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
Author: tkzzzzzz6
3+
Date: 2026-05-26 21:21:16
4+
LastEditors: tkzzzzzz6
5+
LastEditTime: 2026-05-26 21:37:07
6+
'''
7+
#
8+
# @lc app=leetcode.cn id=3120 lang=python3
9+
# @lcpr version=30204
10+
#
11+
# [3120] 统计特殊字母的数量 I
12+
#
13+
14+
15+
# @lcpr-template-start
16+
17+
# @lcpr-template-end
18+
# @lc code=start
19+
class Solution:
20+
def numberOfSpecialChars(self, word: str) -> int:
21+
mask = [0]*2
22+
for c in map(ord,word):
23+
mask[c >> 5 & 1] |= 1<<(c & 31)
24+
25+
return bin(mask[0] & mask[1]).count("1")
26+
# @lc code=end
27+
28+
29+
30+
31+
#
32+
# @lcpr case=start
33+
# "aaAbcBC"\n
34+
# @lcpr case=end
35+
36+
# @lcpr case=start
37+
# "abc"\n
38+
# @lcpr case=end
39+
40+
# @lcpr case=start
41+
# "abBCab"\n
42+
# @lcpr case=end
43+
44+
#
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<!--
2+
* @Author: tkzzzzzz6
3+
* @Date: 2026-05-26 21:26:02
4+
* @LastEditors: tkzzzzzz6
5+
* @LastEditTime: 2026-05-26 21:44:25
6+
-->
7+
# 3120. 统计特殊字母的数量 1
8+
9+
## 题目描述
10+
11+
给一个字符串 `word`。如果 `word` 中同时存在某个字母的小写形式和大写形式,则称这个字母为 **特殊字母**
12+
13+
返回 `word`**特殊字母** 的数量。
14+
15+
## 思路1:求集合的交集
16+
构建两个set,分别表示出现的大写和小写的字母
17+
18+
set 去除重复的元素再求两个集合的交集即可
19+
20+
- 时间复杂度:O(n)
21+
- 空间复杂度:O(n)
22+
23+
### 代码
24+
25+
```py
26+
class Solution:
27+
def numberOfSpecialChars(self, word: str) -> int:
28+
upper = set()
29+
lower = set()
30+
31+
for c in word:
32+
if c == c.lower():
33+
lower.add(c)
34+
else:
35+
upper.add(c.lower())
36+
37+
return len(upper & lower)
38+
```
39+
40+
## 思路2:位运算
41+
42+
注意到大小写字母的ASCII码差值是32,转化为2进制后判断第5位是否是1即可
43+
44+
- A-Z:65-90
45+
- a-z:97-122
46+
47+
两者之间的差别是32
48+
49+
可以采用位运算压缩和存储信息,并使用与位运算求解交集
50+
51+
### 代码
52+
```py
53+
class Solution:
54+
def numberOfSpecialChars(self, word: str) -> int:
55+
mask = [0]*2
56+
for c in map(ord,word):
57+
mask[c >> 5 & 1] |= 1<<(c & 31)
58+
59+
return bin(mask[0] & mask[1]).count("1")
60+
```

leetcode/problems/3120.count-the-number-of-special-characters-i.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
'''
2+
Author: tkzzzzzz6
3+
Date: 2026-05-26 21:21:16
4+
LastEditors: tkzzzzzz6
5+
LastEditTime: 2026-05-26 21:25:35
6+
'''
17
#
28
# @lc app=leetcode.cn id=3120 lang=python3
39
# @lcpr version=30204
@@ -12,8 +18,18 @@
1218
# @lc code=start
1319
class Solution:
1420
def numberOfSpecialChars(self, word: str) -> int:
15-
upper =
21+
upper = set()
22+
lower = set()
23+
24+
for c in word:
25+
if c == c.lower():
26+
lower.add(c)
27+
else:
28+
upper.add(c.lower())
29+
30+
return len(upper & lower)
1631
# @lc code=end
32+
1733

1834

1935

0 commit comments

Comments
 (0)