[Array]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.
思路:将数组中的所有元素值循环移动k步,注意循环的步数并没有说一定小于n!!!!
自己代码:
void rotate(vector<int>& nums, int k) {
vector<int>temp;
for(int i = nums.size()-k%nums.size(); i < nums.size(); i++)
temp.push_back(nums[i]);
for(int i = ; i < nums.size()-k%nums.size(); i++)
temp.push_back(nums[i]);
nums = temp;
}
最后的两个vector之间可以直接赋值。
[Array]189. Rotate Array的更多相关文章
- 189. Rotate Array【easy】
189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...
- <LeetCode OJ> 189. Rotate Array
189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...
- 189. Rotate Array - LeetCode
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 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 ...
- Java for 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 ...
- 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】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 ...
- Java [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 ...
随机推荐
- PropertyPlaceholderConfigurer的注意事项
1.基本的使用方法是<bean id="propertyConfigurerForWZ" class="org.springframework.beans.fact ...
- android 中的一些小问题
1 TextView 在TableRow 中占满一行 要为TextView设置 android:layout_weight="1" 这个属性 2
- Python全栈开发:冒泡排序
#!/usr/bin/env python # -*- coding;utf-8 -*- """ 第一次对比:找到最大值,放到最后 对比是两两对比,对比的两个数组合共有l ...
- css,js文件后面加一个版本号
由于前几天,更新了项目,更新的文件有js文件,今天客人截图过来,我发现修改之后的效果没有显示出来,我回复说清理浏览器缓存.到了晚上,客人找老板,说还没有处理到這个,说客人不懂這个.所以想到之前自己为了 ...
- 廖雪峰Java13网络编程-2Email编程-1发送email
1.邮件发送 1.1传统邮件发送: 传统的邮件是通过邮局投递,从一个邮局到另一个邮局,最终到达用户的邮箱. 1.2电子邮件发送: 与传统邮件类似,它是从用户电脑的邮件软件(如outlook)发送到邮件 ...
- opencv-访问Mat中每个像素的值
参考:[OpenCV]访问Mat中每个像素的值(新) 膜拜大佬 以下例子代码均针对8位单通道灰度图. 1 .ptr和[]操作符 Mat最直接的访问方法是通过.ptr<>函数得到一行的指 ...
- Hack Tools
Tools 2011-03-17 13:54:36| 分类: Security|举报|字号 订阅 Packet Shaper:Nemesis: a command line packet s ...
- day 46 Javascript学习
Javascript学习 JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScri ...
- PAT甲级——A1099 Build A Binary Search Tree
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- c#设置文件及文件夹的属性
c#中通过FileAttributes枚举来设置文件或文件夹的属性. FileAttributes 枚举 成员名称 说明 Archive 文件的存档状态.应用程序使用此属性为文件加上备份或移除标记. ...