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:/ ...
随机推荐
- 用NPOI操作EXCEL关于HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2)的参数
2.4.1 用NPOI操作EXCEL关于HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2)的参数 NPOI教程:http://www.cnb ...
- Oracle数据库之SQL基础(二)
一.约束 ❤ 1.约束概述 约束作用: (1)定义规则 (2)确保完整性:包括数据的精确性.可靠性.以确保数据不会出错,或者尽量减少出错. 约束的类型: (1)非空约束 (2)主键约束 (3)外键约束 ...
- glusterFS安装维护文档
.规划: .依赖包 yum install libibverbs librdmacm xfsprogs nfs-utils rpcbind libaio liblvm2app lvm2-devel l ...
- 关闭ehcache的更新检查UpdateChecker
ehcache默认开启自动更新检查,在你不能联网的环境下,会有异常出现. 在ehcache的配置文件中ehcache标签上加上属性updateCheck="false" 即可.
- PostgreSQL Replication之第九章 与pgpool一起工作(7)
9.7 处理故障转移和高可用 可以使用pgpool来解决的一些明显的问题是高可用性和故障转移.一般来讲,有使用pgpool或者不使用pgpool可以用来处理这些问题的各种方法. 9.7.1 使用Pos ...
- PNG图片去除额外透明区域
bitmapdata.getColorBoundsRect(0xFF000000,0x00000000,false) http://www.cnblogs.com/shinings/archive/2 ...
- :“boost/serialization/string.hpp”: No such file or directory 错误
主要原因是没有安装和配置boost库. 解决:http://www.programlife.net/boost-compile-and-config.html
- html a标签链接使用action 参数传递中文乱码
<a href="queryByType?ptype=鼠标"> 在后台变量ptype接收的值为乱码 解决方法: 在tomcat的server.xml文件中添加 URIE ...
- c# 隐藏 控制台应用程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- [原创]java WEB学习笔记87:Hibernate学习之路-- -映射 继承关系(subclass , joined-subclass,union-subclass )
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...