[leetcode]Rotate Array
in place交换
如果是k步,那么就是把后面k个放到前面了嘛。
我们先把整个数组reverse,然后把前面的reverse回来,再把后面的reverse回来
对于AB我们要通过reverse操作得到BA
那么先把AB reverse一次得到reverse(B)reverse(A)
然后再把reverse(B),reverse(A)分别reverse一次就得到了BA
class Solution {
public:
void rotate(int nums[], int n, int k) {
k = k % n;
reverse(nums, nums + n);
reverse(nums, nums + k);
reverse(nums + k, nums + n);
}
};
[leetcode]Rotate Array的更多相关文章
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- 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 = ...
- LeetCode Rotate Array 翻转数组
题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...
- Python3解leetcode Rotate Array
问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...
- [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 ...
- 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 ...
- LeetCode 189. 旋转数组(Rotate Array)
189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,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 = ...
- <LeetCode OJ> 189. Rotate Array
189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...
随机推荐
- Asp.Net Web API 2第一课——入门
详情请查看http://aehyok.com/Blog/Detail/68.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...
- LoadTest内存和线程Troubleshooting实战
在端午节放假的三天中,我对正在开发的 Service 进行了 LoadTest,尝试在增大压力的条件下发现问题. 该 Service 为独立进程的 WCF 服务,宿主于 WindowsService, ...
- [游戏模版11] Win32 动画 时间消息
>_<:This time we will study a new way to operate your picture.That is running your picture by ...
- Windows 10四大版本区别详解:家庭版, 专业版, 企业版和教育版
Windows 10有四个基本版本:Windows 10 家庭版, Windows 10 专业版, Windows 10 企业版, 和Windows 10 教育版(这是Windows家族的新成员).以 ...
- ActiveMQ第五弹:增加ReDelivery功能
在使用Message Queue的过程中,总会由于种种原因而导致消息失败.一个经典的场景是一个生成者向Queue中发消息,里面包含了一组邮件地址和邮件内容.而消费者从Queue中将消息一条条读出来,向 ...
- Lingo 做线性规划 - Asset allocation and Portfolio models
Reference: <An Introduction to Management Science Quantitative Approaches to Decision Making, Rev ...
- Django时间查询
1.gt:大于某个时间now = datetime.datetime.now()#前一天start = now – datetime.timedelta(hours=23, minutes=59, s ...
- 简单理解ECMAScript2015中的箭头函数新特性
箭头函数(Arrow functions),是ECMAScript2015中新加的特性,它的产生,主要有以下两个原因:一是使得函数表达式(匿名函数)有更简洁的语法,二是它拥有词法作用域的this值,也 ...
- Template Method模式和Strategy模式[继承与委托]
继承 program by difference. 通过继承,可以建立完整的软件结构分层.其中每一层都可以重用该层次以上的Code. 过度使用继承的代价是巨大的.应使用组合或者委托来替代继承. Tem ...
- Android系统分区理解及分区目录细解
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...