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

自己代码:

void rotate(vector<int>& nums, int k) {
vector<int>temp;
for(int i = nums.size()-k%nums.size(); i < nums.size(); i++)
temp.push_back(nums[i]);
for(int i = ; i < nums.size()-k%nums.size(); i++)
temp.push_back(nums[i]);
nums = temp;
}

最后的两个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. 【学术篇】luogu1558&&poj2777 色板游戏

    题目の传送门: luogu:https://www.luogu.org/problem/show?pid=1558 poj:http://poj.org/problem?id=2777 题目大意:给有 ...

  2. 根据table返回来的数据,动态展示组织名称

    <template> <div class="app-container calendar-list-container"> <el-card cla ...

  3. Spring_数据校验和自定义检验规则和分组校验

    @Validated  :绑定需要校验的数据. 数据校验规则:为数据绑定校验的规则 private Long booId;@NotNull(message = "不能为空")pri ...

  4. Lock方法是用于数据库的锁机制,

    Lock方法是用于数据库的锁机制,如果在查询或者执行操作的时候使用: lock(true); 复制代码   就会自动在生成的SQL语句最后加上 FOR UPDATE或者FOR UPDATE NOWAI ...

  5. alias用于设置当前数据表的别名,

    alias用于设置当前数据表的别名,便于使用其他的连贯操作例如join方法等. 示例: $Model = M('User'); $Model->alias('a')->join('__DE ...

  6. vue2 + koa2全栈部署

    1.首先打包前端上传 修改config下的index.js 代理地址为服务器IP index: path.resolve(__dirname, '../dist/index.html'), asset ...

  7. dp练习集

    动态规划(DP) // 以下题目来自牛客网 删括号 f[i][j][k] 表示序列s的前i个匹配序列t的前j个,序列s删除部分左括号与右括号数量差为k的情况是否可行 答案为 f[sl][tl][0] ...

  8. centos部署jeecms

    首先下载安装包apache-tomcat-8.5.40.tar.gz jdk-8u211-linux-x641.rpm jeecmsv9.war 已经在WEB-INF/config/jdbc.prop ...

  9. spark编程入门-idea环境搭建

    原文引自:http://blog.csdn.net/huanbia/article/details/69084895 1.环境准备 idea采用2017.3.1版本. 创建一个文件a.txt 2.构建 ...

  10. Linux-c glib库hash表GHashTable介绍

    百度云glib  链接:https://pan.baidu.com/s/1W9qdlMKWRKIFykenTVuWNQ 密码:ol6y hash表是一种提供key-value访问的数据结构,通过指定的 ...