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相关推荐

  1. 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 ...

  2. [勇者闯LeetCode] 189. Rotate Array

    [勇者闯LeetCode] 189. Rotate Array Description Rotate an array of n elements to the right by k steps. F ...

  3. 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  ...

  4. 【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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. C#LeetCode刷题之#189-旋转数组(Rotate Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3700 访问. 给定一个数组,将数组中的元素向右移动 k 个位置, ...

  9. leetcode python3 简单题189. Rotate Array

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百八十九题 (1)题目 英文: Given an array, rotate t ...

最新文章

  1. 清华思客 | 蓝志勇:人工智能时代公共治理创新迫在眉睫
  2. Kali Linux 2017.1脚本gerix.py修复
  3. 两台服务器安装redis集群_Redis Cluster搭建高可用Redis服务器集群
  4. XSS跨站脚本攻击在Java开发中防范的方法
  5. 统计与机器学习的异同
  6. 10个优秀的Objective-C和iOS开发在线视频教程
  7. Springboot打jar包项目无法访问jsp问题解决
  8. CFA难度:特许金融分析师CFA难考吗?
  9. 移动硬盘无法访问需要格式化,怎样恢复移动硬盘数据
  10. ORCA(Optimal Reciprocal Collision Avoidance)笔记
  11. 端端Clouduolc与百度云盘等公有云同步的区别
  12. linux生成xorg,生成xorg.conf文件
  13. 特殊的搜狗拼音输入法
  14. Java生成指定范围的随机数
  15. Innodb的七种锁
  16. SAP 各个模块简介以及常用的数据表
  17. Timer延时任务和ScheduledThreadPool执行延时任务
  18. MT5中position、order、deal 区别
  19. idog copy from,
  20. 黑马程序员--网络编程知识点总结

热门文章

  1. python操作hbase配置记录-基于thrift2协议
  2. 计算质数通过分区(Partition)提高Spark的运行性能(转载+自己理解)
  3. 如何计算一个神经网络在使用momentum时的hessian矩阵(论文调研)
  4. 登錄CentOS出現-bash-4.1#
  5. args和kwargs以及argv用法
  6. 1.12 四类向量组
  7. php smarty安装,【php】smarty安装
  8. objective-C 自定义对象归档的实现
  9. MVC架构接收jsp页面传值
  10. 22.PATH环境变量