189. Rotate Array【easy】
189. Rotate Array【easy】
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.
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.
解法一:
- class Solution {
- public:
- void rotate(vector<int>& nums, int k) {
- int len = nums.size();
- if (len == || k % len == )
- {
- return;
- }
- int mid = len - k % len;
- reverseArray(nums, , mid - );
- reverseArray(nums, mid, len - );
- reverseArray(nums, , len - );
- }
- void reverseArray(vector<int>& nums, int start, int end)
- {
- int s = start;
- int e = end;
- while (s <= e)
- {
- int temp = nums[s];
- nums[s] = nums[e];
- nums[e] = temp;
- s++;
- e--;
- }
- }
- };
注意:
1、边界值检查,避免对0取余和长度不合法
2、先分别翻转,再总体翻转,注意下标
解法二:
- class Solution
- {
- public:
- void rotate(int nums[], int n, int k)
- {
- k = k%n;
- // Reverse the first n - k numbers.
- // Index i (0 <= i < n - k) becomes n - k - i.
- reverse(nums, nums + n - k);
- // Reverse tha last k numbers.
- // Index n - k + i (0 <= i < k) becomes n - i.
- reverse(nums + n - k, nums + n);
- // Reverse all the numbers.
- // Index i (0 <= i < n - k) becomes n - (n - k - i) = i + k.
- // Index n - k + i (0 <= i < k) becomes n - (n - i) = i.
- reverse(nums, nums + n);
- }
- };
解法三:
- public class Solution {
- public void rotate(int[] nums, int k) {
- k %= nums.length;
- reverse(nums, , nums.length - );
- reverse(nums, , k - );
- reverse(nums, k, nums.length - );
- }
- public void reverse(int[] nums, int start, int end) {
- while (start < end) {
- int temp = nums[start];
- nums[start] = nums[end];
- nums[end] = temp;
- start++;
- end--;
- }
- }
- }
先整体搞,再分开搞
189. Rotate Array【easy】的更多相关文章
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 448. Find All Numbers Disappeared in an Array【easy】
448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...
- 479. Second Max of Array【easy】
Find the second max number in a given array. Notice You can assume the array contains at least two n ...
- 88. Merge Sorted Array【Easy】【双指针-不用额外空间归并两个有序数组】
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
随机推荐
- Hibernate 配置文件precision与scale
Oracle使用标准.可变长度的内部格式来存储数字.这个内部格式精度可以高达38位. NUMBER数据类型可以有两个限定符,如: column NUMBER ( precision, scale) 表 ...
- 【线段树】bzoj3038 上帝造题的七分钟2 / bzoj3211 花神游历各国
暴力修改,记录一段是否全部为1或0,若全是了,则不再修改. 注意3211一定要判是否为0,否则会T得惨无人道. #include<cstdio> #include<cmath> ...
- [TC-HouseProtection]House Protection
题目大意: 一个平面直角坐标系中有给定的$n(n\le50)$个红点和$m(m\le50)$个蓝点,每个点可以选择画一个半径为$r$(所有的$r$相同)的圆或不画.圆的半径上限为$R(R\le1000 ...
- @requestBody注解的使用(上)
1.@requestBody注解常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容,比如说:application/json或者是ap ...
- 13test02:阶乘
//假设32位int型变量y是表示最大人数的x的阶乘,即y=x!,当x最大值取什么时,y取最大值 //,且乘法不溢出. #include<iostream> using namespace ...
- cookie的secure、httponly属性设置
cookie的secure.httponly属性设置 转载自:http://www.cnblogs.com/alanzyy/archive/2011/10/14/2212484.html 一.属性说明 ...
- fidder模拟post提交到PHP遇到的问题
http头必须带上Content-type: application/x-www-form-urlencoded 之后 ,php 才能接收到post数据 1. 用php://input可以很便捷的取 ...
- 在Code First中自动创建Entity模型
之前我在博客文章中介绍过如何使用Code First来创建数据库,通过CodeFirst的方式,可以大幅的减少开发人员的工作量,比传统的先创建库再ORM的方式简单了不少.但是,很多时候,特别是一些MI ...
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:3.安装Oracle RAC-3.2.安装 cvuqdisk 软件包
3.2.安装 cvuqdisk 软件包 3.2.1. 准备Oracle Grid安装包 上传Grid .Oracle 安装文件: sftp> put E:\Software\linux.x64 ...
- Flexbox兼容性语法汇总
Flexbox版本 flexbox从第一次出现至今总共有三个语法版本,他们分别是: "display:box;" — 2009年的老版本 "display:flexb ...