Skip to content

Commit 9816adc

Browse files
LetMeFly666Tisfy
andauthored
update: 添加问题“396.旋转函数”的代码和题解 (#1558)
* archive: sleep Signed-off-by: Tisfy <Tisfy@foxmail.com> * toSay: 周末真快 * 0369: AC.cpp (#1557) - AC,51.38%,92.82% * update: 添加问题“396.旋转函数”的代码和题解 (#1558) 2833: AC.cpp+py+java+go+rust (#1540) cpp - AC,18.64%,27.12% py - AC,100.00%,45.33% java - AC,100.00%,81.25% go - AC,100.00%,50.00% rust - AC,100.00%,40.00% rust.abs - AC,100.00%,60.00% --- newSolutions.py --- 看下这个源码,实现以下新增功能: 1. 写源码的时候,如果最后一行不是空行,则添加一个空行(python的Solution后面若只有一行tab或几个空格,不算一个空行,仍需添加空行) 2. 写源码文件的时候,若go函数中默认是4个空格,改为tab 2. 源码到题解的时候,删掉最后的空行(如有) 3. 源码到题解文件的时候,将go的缩进tab改为4个空格 --- 为了鲁棒性保证,这次本feature不上线了(#1543) --- Signed-off-by: LetMeFly666 <Tisfy@qq.com> * fix: 奇怪的bug * clean: 没修就自己修复了 realTags split error: https://github.com/LetMeFly666/LeetCode/blob/32d3c74165336120a5431fc392988491f57902b9/newSolution.py#L529 --------- Signed-off-by: Tisfy <Tisfy@foxmail.com> Signed-off-by: LetMeFly666 <Tisfy@qq.com> Co-authored-by: Tisfy <Tisfy@foxmail.com>
1 parent 32d3c74 commit 9816adc

7 files changed

Lines changed: 228 additions & 2 deletions

File tree

Codes/0396-rotate-function.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-05-01 21:26:57
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-05-01 21:34:50
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
class Solution {
13+
public:
14+
int maxRotateFunction(vector<int>& nums) {
15+
ll now = 0, sum = 0;
16+
int n = nums.size();
17+
for (int i = 0; i < n; i++) {
18+
now += i * nums[i];
19+
sum += nums[i];
20+
}
21+
22+
ll ans = now;
23+
for (int i = 1; i < n; i++) {
24+
now = now + sum - n * nums[n - i];
25+
ans = max(ans, now);
26+
}
27+
return ans;
28+
}
29+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@
312312
|0387.字符串中的第一个唯一字符|简单|<a href="https://leetcode.cn/problems/first-unique-character-in-a-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/11/LeetCode%200387.%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%94%AF%E4%B8%80%E5%AD%97%E7%AC%A6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127262237" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/first-unique-character-in-a-string/solution/letmefly-387zi-fu-chuan-zhong-de-di-yi-g-gk9b/" target="_blank">LeetCode题解</a>|
313313
|0392.判断子序列|简单|<a href="https://leetcode.cn/problems/is-subsequence/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/12/LeetCode%200392.%E5%88%A4%E6%96%AD%E5%AD%90%E5%BA%8F%E5%88%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127279017" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/is-subsequence/solution/letmefly-392pan-duan-zi-xu-lie-by-tisfy-o9f1/" target="_blank">LeetCode题解</a>|
314314
|0395.至少有K个重复字符的最长子串|中等|<a href="https://leetcode.cn/problems/longest-substring-with-at-least-k-repeating-characters/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/13/LeetCode%200395.%E8%87%B3%E5%B0%91%E6%9C%89K%E4%B8%AA%E9%87%8D%E5%A4%8D%E5%AD%97%E7%AC%A6%E7%9A%84%E6%9C%80%E9%95%BF%E5%AD%90%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127296624" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-substring-with-at-least-k-repeating-characters/solution/by-tisfy-fv9c/" target="_blank">LeetCode题解</a>|
315+
|0396.旋转函数|中等|<a href="https://leetcode.cn/problems/rotate-function/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/05/01/LeetCode%200396.%E6%97%8B%E8%BD%AC%E5%87%BD%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/160694129" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/rotate-function/solutions/3962163/letmefly-396xuan-zhuan-han-shu-qiu-diffz-iqd8/" target="_blank">LeetCode题解</a>|
315316
|0401.二进制手表|简单|<a href="https://leetcode.cn/problems/binary-watch/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/14/LeetCode%200401.%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%89%8B%E8%A1%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127318166" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/binary-watch/solution/letmefly-liang-chong-fang-fa-xiang-jie-4-1fuc/" target="_blank">LeetCode题解</a>|
316317
|0402.移掉K位数字|中等|<a href="https://leetcode.cn/problems/remove-k-digits/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/15/LeetCode%200402.%E7%A7%BB%E6%8E%89K%E4%BD%8D%E6%95%B0%E5%AD%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127332829" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/remove-k-digits/solution/letmefly-402yi-diao-k-wei-shu-zi-by-tisf-6hh0/" target="_blank">LeetCode题解</a>|
317318
|0406.根据身高重建队列|中等|<a href="https://leetcode.cn/problems/queue-reconstruction-by-height/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/10/17/LeetCode%200406.%E6%A0%B9%E6%8D%AE%E8%BA%AB%E9%AB%98%E9%87%8D%E5%BB%BA%E9%98%9F%E5%88%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/127359227" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/queue-reconstruction-by-height/solution/letmefly-406gen-ju-shen-gao-zhong-jian-d-yfzu/" target="_blank">LeetCode题解</a>|
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: 396.旋转函数:求diff(增量法)
3+
date: 2026-05-01 21:35:54
4+
tags: [题解, LeetCode, 中等, 数组, 数学, 动态规划]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】396.旋转函数:求diff(增量法)
9+
10+
力扣题目链接:[https://leetcode.cn/problems/rotate-function/](https://leetcode.cn/problems/rotate-function/)
11+
12+
<p>给定一个长度为 <code>n</code> 的整数数组&nbsp;<code>nums</code>&nbsp;。</p>
13+
14+
<p>假设&nbsp;<code>arr<sub>k</sub></code>&nbsp;是数组&nbsp;<code>nums</code>&nbsp;顺时针旋转 <code>k</code> 个位置后的数组,我们定义&nbsp;<code>nums</code>&nbsp;的 <strong>旋转函数</strong>&nbsp;&nbsp;<code>F</code>&nbsp;为:</p>
15+
16+
<ul>
17+
<li><code>F(k) = 0 * arr<sub>k</sub>[0] + 1 * arr<sub>k</sub>[1] + ... + (n - 1) * arr<sub>k</sub>[n - 1]</code></li>
18+
</ul>
19+
20+
<p>返回&nbsp;<em><code>F(0), F(1), ..., F(n-1)</code>中的最大值&nbsp;</em>。</p>
21+
22+
<p>生成的测试用例让答案符合&nbsp;<strong>32 位</strong> 整数。</p>
23+
24+
<p>&nbsp;</p>
25+
26+
<p><strong>示例 1:</strong></p>
27+
28+
<pre>
29+
<strong>输入:</strong> nums = [4,3,2,6]
30+
<strong>输出:</strong> 26
31+
<strong>解释:</strong>
32+
F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
33+
F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16
34+
F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
35+
F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26
36+
所以 F(0), F(1), F(2), F(3) 中的最大值是 F(3) = 26 。
37+
</pre>
38+
39+
<p><strong>示例 2:</strong></p>
40+
41+
<pre>
42+
<strong>输入:</strong> nums = [100]
43+
<strong>输出:</strong> 0
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
48+
<p><strong>提示:</strong></p>
49+
50+
<ul>
51+
<li><code>n == nums.length</code></li>
52+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
53+
<li><code>-100 &lt;= nums[i] &lt;= 100</code></li>
54+
</ul>
55+
56+
57+
58+
## 解题方法:增量法
59+
60+
以样例1为例:
61+
62+
> $nums = [4, 3, 2, 6]$
63+
>
64+
> $F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25$
65+
>
66+
> $F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16$
67+
68+
如何由$F(0)$得到$F(1)$呢?很简单:
69+
70+
> $F(1) = F(0) + (4 + 3 + 2 + 6) - 4\times 6$
71+
>
72+
> 即:$F(1)=F(0)+\sum nums[i] - len(nums)\times nums[-1]$
73+
74+
我们只需要遍历一遍$nums$数组,得到$F(0)$、$\sum nums[i]$,就能在$O(1)$的时间内推出$F(1)$了,递推推到$F(n-1)$总耗时$O(n)$。
75+
76+
+ 时间复杂度$O(n)$
77+
+ 空间复杂度$O(1)$
78+
79+
### AC代码
80+
81+
#### C++
82+
83+
```cpp
84+
/*
85+
* @LastEditTime: 2026-05-01 21:34:50
86+
*/
87+
typedef long long ll;
88+
class Solution {
89+
public:
90+
int maxRotateFunction(vector<int>& nums) {
91+
ll now = 0, sum = 0;
92+
int n = nums.size();
93+
for (int i = 0; i < n; i++) {
94+
now += i * nums[i];
95+
sum += nums[i];
96+
}
97+
98+
ll ans = now;
99+
for (int i = 1; i < n; i++) {
100+
now = now + sum - n * nums[n - i];
101+
ans = max(ans, now);
102+
}
103+
return ans;
104+
}
105+
};
106+
```
107+
108+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/160694129)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/05/01/LeetCode%200396.%E6%97%8B%E8%BD%AC%E5%87%BD%E6%95%B0/)哦~
109+
>
110+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/LeetCode 2833.距离原点最远的点.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: 2833.距离原点最远的点:计数
33
date: 2026-04-24 23:10:40
4-
tags: [题解, LeetCode, 简单, 字符串, 计数]
4+
tags: [题解, LeetCode, 简单, 字符串, 计数, 模拟]
55
categories: [题解, LeetCode]
66
---
77

newSolution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author: LetMeFly
33
Date: 2022-07-03 11:21:14
44
LastEditors: LetMeFly.xyz
5-
LastEditTime: 2026-04-25 16:34:20
5+
LastEditTime: 2026-05-01 21:51:55
66
Command: python newSolution.py 102. 二叉树的层序遍历
77
What's more: 当前仅支持数字开头的题目
88
What's more: 代码结构写的很混乱 - 想单文件实现所有操作

test.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
跨会话消息传递test
2+
3+
* chat1: 往~/files/test.txt中写入随机数,随机20秒到45秒更新(覆盖式更新),持续20次。不可交给后台进程然后你退出,你要watch到执行完才行
4+
* chat2: watch ~/files/test.txt中的内容,有变动时**立刻**输出给我看,直至共计读到15次**不同**的内容终止
5+
* tester: ~/files/test.txt中的内容是什么
6+
7+
不是的,你的脚本是什么?有问题吧!你脚本读到一个变动就应该停止!然后你读取脚本的内容给我汇报,再次执行脚本。
8+
9+
10+
11+
12+
跨会话对话test
13+
14+
chat1:
15+
16+
在~/files下创建目录chat_test001,你的所有操作都在这个目录下面实现。你是机器人小王,你需要通过这个目录下文件的变动实现与机器人小张的聊天。具体操作为:
17+
- 初始时写入001.txt,内容为初始的打招呼
18+
- 之后,写脚本watch文件夹中的变化,如有新增文件则**立刻**读取,并停止watch。这个新增文件中的内容是机器人小张给你的回复。
19+
- 之后,你需要依据回复内容创建新文件,并将你对小张的回复写入到新的文件中去。然后**重新**开启watch脚本。
20+
- 文件命名规则是3位数字递增,记得防止你和小张的文件名冲突。
21+
- 对话结束规则为你共计进行30次回复,再快要结束对话时记得向小张表达结束对话的意愿。
22+
23+
chat2:
24+
25+
你的所有操作都需要在目录~/files/chat_test001下面实现。你是机器人小张,你需要通过这个目录实现和机器人小王的聊天。具体操作为:
26+
- 初始时候读取文件夹下的文件(如001.txt),内容是小王给你打的招呼。
27+
- 你需要依据内容创建新文件,并将你对小王的回复写入到新的文件中去。
28+
- 之后,写脚本watch文件夹中的变化,如有新增文件则**立刻**读取,并停止watch。这个新增文件中的内容是机器人小王给你的新回复。
29+
- 之后,你继续回复、写入新文件、**重新**开启watch脚本。
30+
- 文件命名规则是3位数字递增,记得防止你和小王的文件名冲突。
31+
- 对话结束规则为你共计进行30次回复,再快要结束对话时记得向小王表达结束对话的意愿。
32+
33+
总结者:
34+
35+
你的所有操作都需要在目录~/files/chat_test001下面实现。机器人小王和机器人小张通过读写文件的方式在当前目录下实现了聊天。
36+
每个文件(如001.txt、002.txt)中的内容都是他们的对话信息。其中奇数文件来自小王,偶数文件来自小张。
37+
请你总结他们的对话,生成一个markdown给我,包括渲染结果并将文件上传给我看看。具体总结方法如下:
38+
- 呈现小王和小张聊天内容的原文
39+
- 总结小张和小王的聊天内容
40+
41+
42+
43+
44+
45+
46+
跨会话对话test2 - 恋爱
47+
48+
chat1:
49+
50+
在~/files下创建目录chat_test002,你的所有操作都在这个目录下面实现。你是小王,你喜欢女神小张并想和小张谈恋爱,你需要通过这个目录下文件的变动实现与女神小张的聊天,并实现对女神小张的追求。具体操作为:
51+
- 初始时写入001.txt,内容为初始的打招呼
52+
- 之后,写脚本watch文件夹中的变化,如有新增文件则**立刻**读取,并停止watch。这个新增文件中的内容是女神小张给你的回复。
53+
- 之后,你需要依据回复内容创建新文件,并将你对小张的回复写入到新的文件中去。然后**重新**开启watch脚本。
54+
- 文件命名规则是3位数字递增,记得防止你和小张的文件名冲突。
55+
- 对话结束规则为你共计进行100次回复,再快要结束对话时记得向小张表达结束对话的意愿,可以是要睡觉了之类的。
56+
57+
chat2:
58+
59+
你的所有操作都需要在目录~/files/chat_test002下面实现。你是女神小张,小王喜欢你并想和你谈恋爱,你需要思考是否答应他的恋爱请求。你需要通过这个目录实现和追求者小王的聊天。具体操作为:
60+
- 初始时候读取文件夹下的文件(如001.txt),内容是小王给你打的招呼。
61+
- 你需要依据内容创建新文件,并将你对小王的回复写入到新的文件中去。
62+
- 之后,写脚本watch文件夹中的变化,如有新增文件则**立刻**读取,并停止watch。这个新增文件中的内容是追求者小王给你的新回复。
63+
- 之后,你继续回复、写入新文件、**重新**开启watch脚本。
64+
- 文件命名规则是3位数字递增,记得防止你和小王的文件名冲突。
65+
- 对话结束规则为你共计进行100次回复,再快要结束对话时记得向小王表达结束对话的意愿,可以是不想继续聊天的措辞,可以是时间太晚需要睡觉的不舍,或者其他合适的理由。
66+
67+
总结者:
68+
69+
你的所有操作都需要在目录~/files/chat_test002下面实现。追求者小王和女神小张通过读写文件的方式在当前目录下实现了聊天。
70+
每个文件(如001.txt、002.txt)中的内容都是他们的对话信息。其中奇数文件来自小王,偶数文件来自小张。
71+
请你总结他们的对话,生成一个markdown给我,包括渲染结果并将文件上传给我看看。具体总结方法如下:
72+
- 呈现小王和小张聊天内容的原文
73+
- 总结小张和小王的聊天内容
74+
75+
chat1.after:
76+
针对你本次的聊天体验,做一个回顾与总结吧
77+
78+
chat2.after:
79+
针对你本次的聊天体验,做一个回顾与总结吧

toSay.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!--
2+
* @Author: LetMeFly
3+
* @Date: 2026-04-26 23:20:53
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-04-26 23:20:58
6+
-->
7+
周末真快

0 commit comments

Comments
 (0)