当前位置:网站首页>LeetCode396.旋转数组
LeetCode396.旋转数组
2022-04-23 08:15:00 【摸鱼の文酱】
| 个人简介 |
️个人主页:摸鱼の文酱博客主页*️
博客领域:java编程基础,mysql
写作风格:干货,干货,还是tmd的干货
精选专栏:【Java】【mysql】 【算法刷题笔记】
博主的码云gitee,平常博主写的程序代码都在里面。
支持博主:点赞、收藏、留言
作者水平很有限,如果发现错误,一定要及时告知作者哦!感谢感谢!
旋转数组
1.原题链接
2.题目要求
给定一个长度为 n 的整数数组 nums 。
假设 arrk 是数组 nums 顺时针旋转 k 个位置后的数组,我们定义 nums 的 旋转函数 F 为:
F ( k ) = 0 ∗ a r r k [ 0 ] + 1 ∗ a r r k [ 1 ] + . . . + ( n − 1 ) ∗ a r r k [ n − 1 ] F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1] F(k)=0∗arrk[0]+1∗arrk[1]+...+(n−1)∗arrk[n−1]
返回 F(0), F(1), …, F(n-1)中的最大值 。
生成的测试用例让答案符合 32 位 整数。
样例输入: nums = [4,3,2,6]
样例输出: 26
解释:
F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25
F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16
F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23
F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26
所以 F(0), F(1), F(2), F(3) 中的最大值是 F(3) = 26 。
3.基础框架
java版本的基础框架代码如下:
class Solution {
public int maxRotateFunction(int[] nums) {
}
}
4.解题思路
1.根据数组旋转我们可以想到将nums复制拼接
2.然后从第一个到第n-1个开始轮流做开头执行那个公式.得到最大前缀和
5.完整代码
class Solution {
//滑动窗口加前缀和
public int maxRotateFunction(int[] nums) {
int n = nums.length;
int[] sum = new int[n * 2 + 10];
for (int i = 1; i <= 2 * n; i++) sum[i] = sum[i - 1] + nums[(i - 1) % n];
int ans = 0;
for (int i = 1; i <= n; i++) ans += nums[i - 1] * (i - 1);
for (int i = n + 1, cur = ans; i < 2 * n; i++) {
cur += nums[(i - 1) % n] * (n - 1);
cur -= sum[i - 1] - sum[i - n];
if (cur > ans) ans = cur;
}
return ans;
}
}
6.涉及算法&总结
滑动窗口 前缀和
版权声明
本文为[摸鱼の文酱]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_53947552/article/details/124348408
边栏推荐
- idea底栏打开services
- The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
- 作文以记之 ~ 二叉树的后序遍历
- form中enctype属性
- 关于cin,scanf和getline,getchar,cin.getline的混合使用
- Ear acupoint diagnosis and treatment essay 0421
- K210学习笔记(二) K210与STM32进行串口通信
- 洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解
- Kubernetes如何使用harbor拉去私有镜像
- JVM工具之Arthas使用
猜你喜欢

Talk about the basic but not simple stock data

Shell脚本进阶

LeetCode-199-二叉树的右视图

Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction

Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code

测试你的机器学习流水线

Knowledge points and problem solutions related to information collection

Goland 调试go使用-大白记录

flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】

根据字节码获取类的绝对路径
随机推荐
How to generate assembly file
数据可视化:使用Excel制作雷达图
ansible自动化运维详解(一)ansible的安装部署、参数使用、清单管理、配置文件参数及用户级ansible操作环境构建
JS中复制数组
洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置
通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解
分布式消息中间件框架选型-数字化架构设计(7)
input元素添加监听事件
[learning] audio and video development from scratch (9) -- nuplayer
JVM工具之Arthas使用
DOM learning notes - traverse all element nodes of the page
虚拟线上展会-线上vr展馆实现24h沉浸式看展
数论求a^b(a,b为1e12级别)的因子之和
Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
什么是RPC
一个必看的微信小程序开发指南1-基础知识了解
stm32以及freertos 堆栈解析
DOM learning - add + - button
洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
lgb,xgb,cat k折交叉验证