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 [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]
Hint:
Could you do it in-place with O(1) extra space?
Related problem: Reverse Words in a String II
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
分析
数组旋转问题。
给定一个序列和一个整数,要求将序列元素右旋k位。题目不难,但是需要注意的一点是k值可能大于序列长度,所以,旋转之前先求k对序列长度size的余数即可。
该题目一个简单的解决方法是三次反转:
- 将序列中(0 , size-k-1)元素反转;
- 将序列中(size-k , size-1)元素反转;
将全序列(0,size-1)反转;
即可完成要求!
AC代码
class Solution {
public:
void rotate(vector<int>& nums, int k) {
if (nums.empty())
return;
int size = nums.size();
//确定最终右旋元素个数
k %= size;
vector<int>::iterator beg = nums.begin();
//旋转0~(size-k) (size-k , size);
reverse(beg, beg + size - k);
reverse(beg + size - k, nums.end());
//最后一次全部反转,即可完成
reverse(beg, nums.end());
}
};
LeetCode(189) Rotate Array的更多相关文章
- LeetCode(61) Rotate List
题目 Given a list, rotate the list to the right by k places, where k is non-negative. For example: Giv ...
- (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(48)Rotate Image
题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
随机推荐
- <pre></pre>标签自动换行
原文地址:https://www.cnblogs.com/qq78292959/p/4193142.html pre { white-space: pre-wrap; word-wrap: bre ...
- NET Core断点续传
.NET Core断点续传 ASP.NET Core断点续传 在ASP.NET WebAPi写过完整的断点续传文章,目前我对ASP.NET Core仅止于整体上会用,对于原理还未去深入学习,由于有 ...
- sql-monitore 的bug 。
http://www.mamicode.com/info-detail-1659243.html 存储过程无法做 sql -monitor , 而存储过程跑的sql (只能通过awr 报告来看sql_ ...
- 042 Trapping Rain Water 接雨水
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算下雨之后能接多少雨水.例如,输入 [0,1,0,2,1,0,1,3,2,1,2,1],返回 6. 详见:https://leetcode.c ...
- 037 Sudoku Solver 解数独
写一个程序通过填充空格来解决数独.空格用 '.' 表示. 详见:https://leetcode.com/problems/sudoku-solver/description/ class Solut ...
- 企业级Web Nginx 服务优化
企业级Web Nginx 服务优化 http://blog.51cto.com/search/result?q=%E4%BC%81%E4%B8%9A%E7%BA%A7Web+Nginx+%E6%9C% ...
- 渣渣菜鸡的 ElasticSearch 源码解析 —— 环境搭建
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/08/25/es-code01/ 软件环境 1.Intellij Idea:2018.2版本 2. ...
- express转发请求
express var express = require('express'); var axios = require('axios'); var qs = require('qs'); var ...
- datatables后台分页例子(可直接复制代码)
1.head表签引用 这两个文件即可 2.复制下面的代码到webform中的head标签中 <script> $(function () { //提示信息 var lang = { &qu ...
- 使用js获取复选框的值,并把数组传回后台处理,过程使用的是Ajax异步查询
这是界面代码: function shua(){ var id_array=new Array(); $('input[id="checkAll& ...