LeetCode题解之Rotate Array
1、题目描述
2、代码
void rotate(vector<int>& nums, int k) { if( k == )
return ;
if( k % nums.size() == && (k / nums.size()) % == )
reverse( nums.begin(), nums.end() ); k %= nums.size() ; vector<int> ans;
auto rb = nums.rbegin();
stack<int> s;
for( int i = k; i > ; i--,rb++)
{
s.push(*rb);
} while( !s.empty() )
{
int tmp = s.top();
ans.push_back(tmp);
s.pop(); }
auto b = nums.begin();
for( int j = ; j < nums.size() - k; j++,b++)
{
ans.push_back(*b);
}
copy(ans.begin(),ans.end(),nums.begin()); }
LeetCode题解之Rotate Array的更多相关文章
- 【LeetCode题解】数组Array
1. 数组 直观地看,数组(Array)为一个二元组<index, value>的集合--对于每一个index,都有一个value与之对应.C语言中,以"连续的存储单元" ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【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 ...
- LeetCode OJ 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 ...
- LeetCode算法题-Rotate Array(Java实现)
这是悦乐书的第184次更新,第186篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第43题(顺位题号是189).给定一个数组,将数组向右旋转k步,其中k为非负数.例如: ...
- LeetCode题解:Rotate List
Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For exa ...
- (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 ...
- LeetCode OJ: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 题解之Rotate List
1.题目描述 2.题目分析 首先数出链表中的元素个数count,然后对k = k % count 求余数.接着将链表形成一个环,从最后一个元素开始,运动 count - k 步,此时节点的下一个节点 ...
随机推荐
- 原来你一直写错了?!实力分享一波 CSS 使用的书写规范顺序与偏门又实用的样式...
我们在埋头写代码的时候,还要学会收集整理一些常用的代码小技巧,以便在工作时候,可以及时调取,提高工作效率. 今天,我把之前收集整理的一些CSS代码小技巧分享出来,供你参考学习,希望对你有帮助. 一.C ...
- 解决Nginx 504 Gateway Time-out问题
解决方案:在http里设置FastCGI相关参数,如: worker_processes 1; events { worker_connections 1024; } http { include m ...
- java.lang 类String
方法摘要1 charcharAt(int index) 返回指定索引处的 char 值. index - char 值的索引.2 string concat( ...
- Java数据库连接池原理与简易实现
1.什么是数据库连接池 我们现在在开发中一定都会用到数据库,为了提高我们的系统的访问速度,数据库优化是一个有效的途径.我们现在开发中使用数据库一般都要经历以下的四个步骤:(1)加载数据库的驱动类,(2 ...
- for-in循环
//for in循环遍历var objs={"username":"hh","age":"20","sex&q ...
- JS的可枚举性
在学习ES6的过程中,涉及到遍历方法时,提到过可枚举性,且多种遍历方法都与可枚举性相关.本章节,将总结这些遍历方法的可枚举性,并在必要的部分,给出对比实例. 一.设置属性的可枚举性 在上一文章 ...
- lua闭包与简易迭代器实现
1.什么是闭包 闭包,又称闭合函数(closure).通常,如果将一个函数写在另一个函数内,那么这个在内部的函数就可以访问到外部函数中的局部变量,这个特征就是词法域,有些资料上也叫它词法定界.闭包指的 ...
- Node.js自定义对象事件监听与发射
一.Node.js是以事件驱动的,那我们自定义的一些js对象就需要能监听事件以及发射事件.在Node.js中事件使用一个EventEmitter对象发出,该对象在events模块中.它应该是使用观察者 ...
- *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<WKWebViewConfiguration 0x1701bcd20> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the k
问题描述: ionic项目,windows下正常,打包android可正常运行: 因为需要打包到iPhone (ios 11.0.1)上测试,将代码拿到Mac OS环境下(重新npm install. ...
- 我用ASP.NET缓存之OutputCache
[我的理解]页面缓存常用在网站上.Web应用系统上也用,但由于Web系统常与数据库打交道.时效性要求蛮强的,所以是否能用缓存得具体情况具体分析(很喜欢这句话“具体情况具体分析”,很符合国人的中庸之道) ...