当前位置:网站首页>leetcode:189. 轮转数组
leetcode:189. 轮转数组
2022-08-03 16:05:00 【OceanStar的学习笔记】
题目来源
题目描述

题目解析
使用临时数组
可以用一个临时数组,先把原数组的值存放到一个临时数组中,然后再把临时数组的值重新赋给原数组,重新赋值的时候要保证每个元素都要后移k位,如果超过数组长度就重头开始

class Solution {
public:
// 1 <= nums.length <= 10^5
void rotate(vector<int>& nums, int k) {
int N = nums.size();
k = k % N;
if(k == 0){
return;
}
std::vector<int> temp(N);
//把原数组值放到一个临时数组中,
for (int i = 0; i < N; i++) {
temp[i] = nums[i];
}
//然后在把临时数组的值重新放到原数组,并且往右移动k位
for (int i = 0; i < N; i++) {
nums[(i + k) % N] = temp[i];
}
}
};
多次反转

class Solution {
public:
// 1 <= nums.length <= 10^5
void rotate(vector<int>& nums, int k) {
int N = nums.size();
k = k % N;
if(k == 0){
return;
}
std::reverse(nums.begin(), nums.end());
std::reverse(nums.begin(), nums.begin() + k);
std::reverse(nums.begin() + k, nums.end());
}
};
连环怼

class Solution {
public:
// 每次把一个元素弄到对应位置去
// 1 <= nums.length <= 10^5
void rotate(vector<int>& nums, int k) {
int N = nums.size();
k = k % N;
int count = 0; // 记录交换位置的次数,n个同学一共需要换n次
for (int start = 0; count < N; ++start) {
int curr = start; // 从0位置开始换位子
int prev = nums[curr];
do{
//下一个坐标
int next = (curr + k) % N;
//交换
int tmp = nums[next];
nums[next] = prev;
prev = tmp;
//更新当前位置
curr = next;
count++;
}while (start != curr); // 循环暂停,回到起始位置,角落无人
}
}
};
边栏推荐
- How much do you know about the intelligent operation and maintenance service of data warehouse based on DMS?
- 一文看懂推荐系统:概要02:推荐系统的链路,从召回粗排,到精排,到重排,最终推荐展示给用户
- 请问下,flink cdc监控oracle,我看源码是通过sid方式的,请问怎么改成service
- 如何选择合适的损失函数,请看......
- 泰山OFFICE技术讲座:段落边框的绘制难点在哪里?
- 技术干货|如何将 Pulsar 数据快速且无缝接入 Apache Doris
- Difference and performance comparison between HAL and LL library of STM32
- 美国国防部更“青睐”光量子系统研究路线
- Not to be ignored!Features and advantages of outdoor LED display
- 不可忽略!户外LED显示屏的特点及优势
猜你喜欢

《安富莱嵌入式周报》第276期:2022.07.25--2022.07.31

Small Tools(4) 整合Seata1.5.2分布式事务

Interpretation of the 2021 Cost of Data Breach Report

用友YonSuite与旺店通数据集成对接-技术篇2
![[QT] Qt project demo: data is displayed on the ui interface, double-click the mouse to display specific information in a pop-up window](/img/3f/265c9d2703056260e03c346fa65a03.png)
[QT] Qt project demo: data is displayed on the ui interface, double-click the mouse to display specific information in a pop-up window

【深度学习】今日bug(8月2)
![[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 01](/img/8e/fcf79d150af4384c14a118fb209725.png)
[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 01

To participate in sweepstakes, incoming new programmers magazine welfare!

DAYU200 OpenHarmony标准系统HDMI全屏显示

人脸识别损失函数的汇总 | Pytorch版本实现
随机推荐
基于DMS的数仓智能运维服务,知多少?
将 Windows 事件日志错误加载到 SQL 表中
How to start an NFT collection
20. Valid Parentheses
托尔斯泰:生活中只有两种不幸
Fortinet产品导入AWS AMI操作文档
请问下阿里云全托管flink能执行两条flink sql命令么?
[QT] Qt project demo: data is displayed on the ui interface, double-click the mouse to display specific information in a pop-up window
leetcode: 899. Ordered Queue [Thinking Question]
2021年数据泄露成本报告解读
posgresql 到 es 报这个错误 ,啥意思
基于DMS的数仓智能运维服务,知多少?
世界顶级级架构师编写2580页DDD领域驱动设计笔记,属实有牌面
Difference and performance comparison between HAL and LL library of STM32
【Unity入门计划】基本概念(8)-瓦片地图 TileMap 02
【Unity入门计划】基本概念(7)-Input Manager&Input类
带你了解什么是 Web3.0
EA 改口,称单人游戏是产品组合中“非常重要的一部分”
QT QT 】 【 to have developed a good program for packaging into a dynamic library
MySQL窗口函数 PARTITION BY()函数介绍