rotate array 旋转数组
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n=nums.size();
int i=0;
//-------------------
//解法一 会超时
//--------------------
k=k%n;
while(i<k){
int temp=nums[n-1];
for(int j=n-1;j>0;j--){
nums[j]=nums[j-1];
}
nums[0]=temp;
i++;
}
//-------------------
//解法二
//--------------------
vector<int> num1; 新开一个数组,存放
k=k%n;
if(k==0) return;
while(i<k)
{
num1.push_back(nums[n-k+i]);
i++;
}
i=0;
while(i<n-k)
{
num1.push_back(nums[i]);
i++;
}
i=0;
while(i<n)
{
nums[i]=num1[i];
i++;
}
}
//-------------------
//解法三 三次旋转;很巧妙
//--------------------
};
rotate array 旋转数组的更多相关文章
- [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] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- Rotate Array 旋转数组 JS 版本解法
Given an array, rotate the array to the right by k steps, where k is non-negative. 给定一个数组,并且给定一个非负数的 ...
- 189 Rotate Array 旋转数组
将包含 n 个元素的数组向右旋转 k 步.例如,如果 n = 7 , k = 3,给定数组 [1,2,3,4,5,6,7] ,向右旋转后的结果为 [5,6,7,1,2,3,4].注意:尽可能找 ...
- LeetCode Rotate Array 翻转数组
题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...
- 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 ...
- rotate image(旋转数组)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 33. Search in Rotated Sorted Array旋转数组二分法查询
一句话思路:反正只是寻找一个最小区间,断开也能二分.根据m第一次的落点,来分情况讨论. 一刷报错: 结构上有根本性错误:应该是while里面包括if,不然会把代码重复写两遍,不好. //situati ...
- 153 Find Minimum in Rotated Sorted Array 旋转数组的最小值
假设一个按照升序排列的有序数组从某未知的位置旋转.(比如 0 1 2 4 5 6 7 可能变成 4 5 6 7 0 1 2).找到其中最小的元素.你可以假设数组中不存在重复的元素.详见:https:/ ...
随机推荐
- 通过ping确定网卡mtu
使用 ping 测试确定网卡最佳 MTU 的方法 MTU ( Maximum Transmission Unit ,最大传输单元)是指某一层协议上一次能最大传输的数据量.当一个数据包超过 MTU 数据 ...
- 5、JavaScript进阶篇②——函数、事件、内置对象
一.函数 1. 什么是函数 函数的作用,可以写一次代码,然后反复地重用这个代码. 如:我们要完成多组数和的功能. var sum; sum = 3+2; alert(sum); sum=7+8 ; a ...
- [3D]绘制线
数据实体: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sy ...
- [MVCSharp]开始使用MVC#
Getting started with MVC# framework The source code of this example can be found under "Example ...
- Strawberry Perl CPAN Install Module 安装 Module 方法
我在 Windows 上用的是 Strawberry Perl. 安装好 Strawberry Perl 之后, 打开 CMD, 输入 CPAN <Module_Name> 即可安装你想要 ...
- Can't add self as subview的困惑
2016-05-27 09:40:43.4043 UMLOG: event: session_id=B63F36D84AD478B9F95C7D0F05DD819B, event=user_choos ...
- isKindOfClass和isMemberOfClass
https://github.com/ming1016/study/wiki/Objc-Runtime 先看看isKindOfClass和isMemberOfClass在Object.mm中的实现 - ...
- linux:ACL权限
ACL权限是为了防止权限不够用的情况,一般的权限有所有者.所属组.其他人这三种,当这三种满足不了我们的需求的时候就可以使用ACL权限: 比如:一个网络老师,给一个班的学员上课,他在linux的根目录下 ...
- 一个新人对于JavaScript简单应用的理解
JavaScript 1.输出:document.write("hello,world"); document.write的意思就是给我再此页面中显示出什么什么小括号里面的内 ...
- Ruby On Rails环境搭建
注:现在http://rubyforge.org 网站已经停止运行,取而代之的是https://rubygems.org这个网站,下文中所需要的gem包都可以去这个网站搜索下载.其他完全按照下文说的去 ...