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. Django源码分析之执行入口

    魔法门 一般我们启动django,最简单的方法是进入project 目录,这时目录结构是这样的 然后我们执行python manage.py runserver,程序就开始执行了. 那django是如 ...

  2. 对TPR(真正例率) 与 FPR(反正例率)的理解

    将测试样本进行排序,“最可能”是正例的排在最前面,“最不可能”是正例的排在最后面. 分类过程就相当于在这个排序中以某个“截断点”(见图中阈值)将样本分为两部分,前一部分判作正例,后一部分判作反例. 我 ...

  3. Android之内容提供者ContentProvider的总结

    本文包含以下知识点: ContentProvider Uri 的介绍 ContentResolver: 监听ContentProvider的数据改变 一:ContentProvider部分 Conte ...

  4. DM8168通过GPMC接口与FPGA高速数据通信实现

    硬件:TI达芬奇TMS320DM8168(以下简称DSP).EP4CE6E22C8N(以下简称FPGA) 软件:linux-2.6.37 转载请注明出处- http://www.cnblogs.com ...

  5. windows下git hub的GUI软件配置与使用

    转载自:http://www.cnblogs.com/haore147/p/3618930.html   1. 安装两个软件 1 2 1. git的命令行程序--git for windows:htt ...

  6. SourceTree git的管理工具使用教程1

    1SourceTree是一个window系统下的Git管理工具 2设置Git 工具——选项——Git设置 3拷贝远程的项目 新建/克隆(输入远程项目的url地址) 4验证(填写用户信息) 工具——选项 ...

  7. elasticsearch this is not a http port

    访问的是elastic search的tcp端口,需换成http端口. elastic search默认tcp端口9300,http端口9200 如果浏览器中访问http://localhost:92 ...

  8. Windows IRP

    IRP(I/O Request Packet),是由IO manager发起的对device的IO请求. 当用户调用系统API,如createFile类似的函数,其实是会交给IO manager来做相 ...

  9. 匿名内存 | shm

    page_is_file_cache是和swapbacked互斥的,所以说对于匿名页来说,分配的时候就就会把PageSwapBacked给设置上,page->mapping_address = ...

  10. can be found for element 'tx:annotation-driven'

    错误描述: ERROR [ContainerBackgroundProcessor[StandardEngine[Catalina]]] (ContextLoader.java:308) - Cont ...