18-Rotate Array-Leetcode
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.
[show hint]
Related problem: Reverse Words in a String II
思路一:定位具体位置,保存至另一容器,时间O(n),空间O(n)
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n=nums.size();
if(k>n)k=k%n;
vector<int> tmp;
for(int i=n-k;i<n;++i)
{
tmp.push_back(nums[i]);
}
for(int i=0;i<n-k;++i)
{
tmp.push_back(nums[i]);
}
nums = tmp;
}
};
思路二:全部逆转,再分别逆转两部分,O(n),空间O(1)
18-Rotate Array-Leetcode的更多相关文章
- 189. Rotate Array - LeetCode
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...
- Rotate Array leetcode
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- LeetCode: Reverse Words in a String && Rotate Array
Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe
Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...
- LeetCode 189. 旋转数组(Rotate Array)
189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- LeetCode Rotate Array 翻转数组
题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...
- <LeetCode OJ> 189. Rotate Array
189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy Rotate an array o ...
- Python3解leetcode Rotate Array
问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...
随机推荐
- Noip模拟54 2021.9.16
T1 选择 现在发现好多题目都是隐含的状压,不明面给到数据范围里,之凭借一句话 比如这道题就是按照题目里边给的儿子数量不超过$10$做状压,非常邪门 由于数据范围比较小,怎么暴力就怎么来 从叶子节点向 ...
- 万能构造解决Rolle中值问题
只要原函数是两个函数的乘积形式,皆可此构造.
- 洛谷 P3195 [HNOI2008] 玩具装箱
链接: P3195 题意: 给出 \(n\) 个物品及其权值 \(c\),连续的物品可以放进一个容器,如果将 \(i\sim j\) 的物品放进一个容器,产生的费用是 \(\left(j-i+\sum ...
- Bzoj P2054 疯狂的馒头 | 并查集
题目链接 思路:因为每次染色都会将某些馒头的颜色彻底更改,所以每个馒头的最终的颜色其实是由最后一次染色决定的,那么我们只考虑最后一次染色即可.对此,我们可以从后往前倒着染色,当目前的染色区间中存在白色 ...
- 用C++实现的数独解题程序 SudokuSolver 2.7 及实例分析
引言:一个 bug 的发现 在 MobaXterm 上看到有内置的 Sudoku 游戏,于是拿 SudokuSolver 求解,随机出题,一上来是个 medium 级别的题: 073 000 060 ...
- pycharm软件安装和破解
pycharm安装 1. 进入pycharm的官网 --- 下载专业版的pycharm 2. 双击下载好的软件,下一步 3. 选择需要安装软件的路径 --- 注意: 尽量不要将软件装在C盘里 4. 默 ...
- HDC2021:HMS Core分析服务,数智化营销闭环方案帮助开发者实现精益增长
10.22-10.24华为开发者大会2021(Together)在东莞如期举行.本次大会上,HMS Core华为分析服务作为多平台.跨设备的一站式数据分析平台以数据驱动业务智能决策为理念,带来了数智化 ...
- 浅谈对typora的使用
内容概要 - 什么是typora - typora的具体使用 目录 内容概要 - 什么是typora - typora的具体使用 1. 什么是typora 2.typora的具体使用 1.标题级别 2 ...
- 【mysql1】如何删除MySQL内存|不再跟新系列
完全卸载mysql的具体步骤: 包括停止服务 + 卸载相关程序 + 删除注册表等等 步骤一: windows键+R-->Control-->程序和功能:所有MySQL程序点击右键 ...
- Bootstrap-2栅格系统
栅格系统(使用最新版本bootstrap) Grid options(网格配置) Responsive classes(响应式class) Gutters(间距) Alignment(对齐方式) Re ...