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.

[show hint]

Credits: Special thanks to @Freezen for adding this problem and creating all test cases.

这是一个一位数组的简单处理,但是也凸显出自己在这方面思路匮乏。以下是一个时间复杂度为0(n^2)的不太好的解法,还在绞尽脑子的像一个快速的交换算法,以下的这个只能给60分。

另外送自己一句话,编程有兴趣,就要勤奋,不能三天打鱼两天晒网。

class Solution
{
public:
void rotate(int num[], int n, int k)
{
int iTemp = 0;
for(int iStep = 0; iStep != k; ++ iStep)
{
iTemp = num[n-1];
for(int i = n-1; i!=0; --i)
{
num[i] = num[i-1];
}
num[0] = iTemp;
}
}
};

  这种弱爆了的状态还需要下一番功夫解决,多看书,多写程序!!!

LeetCode189:Rotate Array的更多相关文章

  1. 理解JavaScript中的参数传递 - leetcode189. Rotate Array

    1.关于leetcode 这是第一篇关于leetcode的题解,就先扯点关于leetcode的话. 其实很早前就在博客园看到过leetcode一些题解,总以为跟一般OJ大同小异,直到最近点开了一篇博文 ...

  2. LeetCode189——Rotate Array

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

  3. Leetcode-189 Rotate Array

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

  4. LeetCode 189. 旋转数组(Rotate Array)

    189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...

  5. 回文数组(Rotate Array (JS))

    旋转一个数组. function rotate(array,n){ var l =array.length,a=array.map(function(x){return x}),arr=[]; n=n ...

  6. 【LeetCode】Rotate Array

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

  7. LeetCode: Reverse Words in a String && Rotate Array

    Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...

  8. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  9. 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

    Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...

随机推荐

  1. Word2vec之CBOW

    一.Word2vec word2vec是Google与2013年开源推出的一个用于获取word vecter的工具包,利用神经网络为单词寻找一个连续向量看空间中的表示.word2vec是将单词转换为向 ...

  2. sqlserver2012 查询远程数据库

    EXEC sp_addlinkedserver 'LinkName','','SQLOLEDB','121.43.177.236'EXEC sp_addlinkedsrvlogin 'LinkName ...

  3. winform 控件半透明设置

    1.backcolor属性为color.FromArgb(100, 220, 220, 220); 2.全透明设置为transparent方法.

  4. 使用Microsoft EnterpriseLibrary(微软企业库)日志组件把系统日志写入数据库和xml文件

    这里只是说明在项目中如何配置使用微软企业库的日志组件,对数据库方面的配置请参考其他资料. 1.在项目中添加Microsoft.Practices.EnterpriseLibrary.Data.dll. ...

  5. Mybatis学习系列(一)入门简介

    MyBatis简介 Mybatis是Apache的一个Java开源项目,是一个支持动态Sql语句的持久层框架.Mybatis可以将Sql语句配置在XML文件中,避免将Sql语句硬编码在Java类中.与 ...

  6. 【PHP】- Apache设置

    Apache配置 1.首先新建一个自己的amp目录(模仿wampserver安装目录),以后的apache,mysql,php都放在此目录下. 2.下载apache 根据自己的系统下载相应的压缩包,我 ...

  7. php在类里如何调用call_user_func_array《细说php2》

  8. java 自定义序列化

    pom.xml 导包 创建自己的序列化类,继承 com.fasterxml.jackson.databind.JsonSerializer<T> 抽象类 重写 serialize() 方法 ...

  9. 【python】python 中的三元表达式(三目运算符)

    python中的三目运算符不像其他语言其他的一般都是 判定条件?为真时的结果:为假时的结果 如 result=5>3?1:0 这个输出1,但没有什么意义,仅仅是一个例子.而在python中的格式 ...

  10. 【题解】洛谷P2418 yyy loves OI IV

    感觉很是妙啊……这题数次误入歧途...最开始想的二维dp,单调队列优化:无果,卒.于是没忍住看了下标签:暴力枚举?搜索?于是开始想记忆化搜索.以为会有什么很强的剪枝之类的:30分,卒.最后终于回到正道 ...