HappyLeetcode50:Rotate Array
Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7]
is rotated to [5,6,7,1,2,3,4]
.
这道题虽然做了出来,但是显然不是最优解。显然空间要求没有达到。
有一点要尤为注意,在一开始的时候我就默认k是小于n的,实际上k可以大于n,所以在修改代码之后,我们所使用的k要对n取余。
我的思路是,把数组中前面的值先挪到后面去。后面的值在被替换之前先存进一个vector里面去,待前面的值安排完毕之后,把vector之中的数据取出,放在新数组的前面。
代码如下:
class Solution { public:void rotate(int nums[], int n, int k) {if (nums == NULL || n <= 0 )return;k = k%n;vector<int> TempSave;TempSave.reserve(k);int count = 0;//记录已经推进了多少个数据for (int i = n - k - 1; i >= 0; --i){if (count < k){TempSave.push_back(nums[i + k]);count++;}nums[i + k] = nums[i];}int Temp = TempSave.size();if (TempSave.size() < k){for (int i = k - 1; i >= Temp; --i)TempSave.push_back(nums[i]);}for (int i = 0; i < k; ++i){nums[i] = TempSave.back();TempSave.pop_back();} } };
其实后来想想还是有更简单一些的方法,比如先把数组复制一遍,再根据情况裁剪出你需要的那一部分数组出来。
class Solution { public:void rotate(int nums[], int n, int k) {k = k % n;int* NewNums =new int[n * 2];for (int i = 0; i < n ; ++i){NewNums[i] = nums[i];NewNums[i + n] = nums[i];}//int res = n - k;for (int i = 0; i < n; ++i){nums[i] = NewNums[i + n - k];}} };
代码量明显短了不少。
转载于:https://www.cnblogs.com/chengxuyuanxiaowang/p/4302957.html
HappyLeetcode50:Rotate Array相关推荐
- leetcode-189. Rotate Array
189. Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and ...
- [勇者闯LeetCode] 189. Rotate Array
[勇者闯LeetCode] 189. Rotate Array Description Rotate an array of n elements to the right by k steps. F ...
- LeetCode Rotate Array(数组的旋转)
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- 【LeetCode从零单排】No189 .Rotate Array
题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...
- Leet Code OJ 189. Rotate Array [Difficulty: Easy]
题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...
- LeetCode之Rotate Array
1.题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the a ...
- LeetCode 189. Rotate Array
题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...
- C#LeetCode刷题之#189-旋转数组(Rotate Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3700 访问. 给定一个数组,将数组中的元素向右移动 k 个位置, ...
- leetcode python3 简单题189. Rotate Array
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百八十九题 (1)题目 英文: Given an array, rotate t ...
最新文章
- 清华思客 | 蓝志勇:人工智能时代公共治理创新迫在眉睫
- Kali Linux 2017.1脚本gerix.py修复
- 两台服务器安装redis集群_Redis Cluster搭建高可用Redis服务器集群
- XSS跨站脚本攻击在Java开发中防范的方法
- 统计与机器学习的异同
- 10个优秀的Objective-C和iOS开发在线视频教程
- Springboot打jar包项目无法访问jsp问题解决
- CFA难度:特许金融分析师CFA难考吗?
- 移动硬盘无法访问需要格式化,怎样恢复移动硬盘数据
- ORCA(Optimal Reciprocal Collision Avoidance)笔记
- 端端Clouduolc与百度云盘等公有云同步的区别
- linux生成xorg,生成xorg.conf文件
- 特殊的搜狗拼音输入法
- Java生成指定范围的随机数
- Innodb的七种锁
- SAP 各个模块简介以及常用的数据表
- Timer延时任务和ScheduledThreadPool执行延时任务
- MT5中position、order、deal 区别
- idog copy from,
- 黑马程序员--网络编程知识点总结