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:/ ...
随机推荐
- 7z usecaes
1. Archive without compressing 7z a -t7z -mx=0 OutputFilename InputFilename Descryption: a: command, ...
- 阿里云数据库实例的一个db被开发人员删除了 如何恢复
1没有 逻辑备份的话. 如下操作即可 可以将那个临时实例的需要导的db用逻辑备份出来恢复到主实例就行了 好多朋友都在问,RDS中把数据恢复到7天内任意时间点的功能在哪里啊? 其实挺简单的,只需要五步操 ...
- iOS的三种多线程技术NSThread/NSOperation/GCD
1.iOS的三种多线程技术 1.NSThread 每个NSThread对象对应一个线程,量级较轻(真正的多线程) 2.以下两点是苹果专门开发的"并发"技术,使得程序员可以不再去关心 ...
- django上传下载大文件
上传 def upFile(file): upload_dir = '/tmp/upload/%s' % username if request.method == 'POST': upload_fi ...
- 使用duplicate target database ... from active database复制数据库
使用duplicate target database ... from active database复制数据库 source db:ora11auxiliary db:dupdb 1.修改监听文件 ...
- linux:centos准备及安装
1>.安装前准备(将虚拟机和映像文件iso下载好) 1.1>.centos下载(建议使用Filezilla下载(http://filezilla-project.org/download. ...
- [Reprint]c++ 析构函数的调用
析构函数在调用默认的析构函数和用户自己覆写的析构函数的时候有点意识模糊呢.写段代码总结下 #include <iostream> using namespace std; class Bo ...
- .NET: C#: Datetime
比较简单的类,一般用到它的属性.经常会用到的是DateTime.Now和DateTime.Now.TimeOfDay; using System; using System.Collections.G ...
- poj: 3006
简单题 #include <iostream> #include <stdio.h> #include <string> #include <stack> ...
- eclipse的debug模式启动缓慢
这个问题可能是由于eclipse和服务器的交互而产生的,在以debug模式启动服务器时,发生了读取文件错误,eclipse自动设置了断点,导致服务器不能正常启动. 解决方法如下:以debug模式启 ...