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

Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.

思路:将数组中的所有元素值循环移动k步,注意循环的步数并没有说一定小于n!!!!

自己代码:

  1. void rotate(vector<int>& nums, int k) {
  2. vector<int>temp;
  3. for(int i = nums.size()-k%nums.size(); i < nums.size(); i++)
  4. temp.push_back(nums[i]);
  5. for(int i = ; i < nums.size()-k%nums.size(); i++)
  6. temp.push_back(nums[i]);
  7. nums = temp;
  8. }

最后的两个vector之间可以直接赋值。

[Array]189. Rotate Array的更多相关文章

  1. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  2. &lt;LeetCode OJ&gt; 189. Rotate Array

    189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...

  3. 189. Rotate Array - LeetCode

    Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...

  4. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  5. 189. Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  6. Java for 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 array ...

  7. 189. Rotate Array -- 将数组前一半移到后一半

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...

  8. 【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 array  ...

  9. Java [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 ...

随机推荐

  1. Minifilter 相关

    FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO 商用软件一定要过滤掉这个类型的请求,这个类型的请求响应非常慢. FLTFL_OPERATION_REGISTRA ...

  2. nginx i.com.conf

    server { listen 9090; server_name i.com; root /Users/chong/Documents/www; # Load configuration files ...

  3. display: -webkit-box; 做个小小试验

    最近做个微信项目发现css3在微信内部浏览器中和其他浏览有些区别 做个小小笔记 .job { display: -webkit-box; display: flexbox; -webkit-box-p ...

  4. 黑阀 adb 命令

    adb 命令 adb -d shell sh /data/data/me.piebridge.brevent/brevent.sh

  5. 下面分享一下RHEL/CentOS7 安装图形化桌面详细图解

    Linux是一个多任务的多用户的操作系统,好多linux爱好者在安装完linux后经常遇到一个问题——没有图形化桌面(http://www.xcmnyy.com)今天小编在安装RHEL7的时候,一步留 ...

  6. 容斥原理——hdu3208

    和hdu2204有点像 这题要特别注意精度问题,如pow的精度需要自己搞一下,然后最大的longlong可以设为1<<31 /* 只要求[1,n]范围内的sum即可 那么先枚举幂次k[1, ...

  7. Less适配移动端rem

    @ue-width: 750; /* 设计图的宽度 */ .px2rem(@px) { @remValue: @px/@ue-width*10; @pxToRem: ~"@{remValue ...

  8. webGL动画

    在做这个项目之前,我也和很多人的想法一样觉得:H5做动画性能不行,只能完成简单动画,可是事实并非如此.所以借此篇分享振奋下想在H5下做酷炫游戏的人心. 体验游戏请长按二维码识别: 好吧,知道你懒.不想 ...

  9. elasticsearch 中文API facets(⑩)

    facets Elasticsearch提供完整的java API用来支持facets.在查询的过程中,将需要计数的facets添加到FacetBuilders中.然后将该FacetBuilders条 ...

  10. Vuex持久化存储之vuex-persist

    在引入mapMutations时报错,解决方法: 1:npm install --save-dev babel-plugin-transform-object-rest-spread 2:在packa ...