【easy】189. Rotate Array
题目标签:Array
题目给了我们一个数组 和 k。 让我们 旋转数组 k 次。
方法一:
这里有一个很巧妙的方法:
利用数组的length - k 把数组 分为两半;
reverse 左边和右边的数组;
reverse 总数组。
举一个例子:
1 2 3 4 5 6 7 如果k = 3 的话, 会变成 5 6 7 1 2 3 4
1 2 3 4 5 6 7 middle = 7 - 3 = 4,分为左边 4个数字,右边 3个数字
4 3 2 1 7 6 5 分别把左右reverse 一下
5 6 7 1 2 3 4 把总数组reverse 一下就会得到答案
public class Solution
{
public void rotate(int[] nums, int k)
{
if(nums == null || nums.length == || k % nums.length == )
return; int turns = k % nums.length;
int middle = nums.length - turns; reverse(nums, , middle-); // reverse left part
reverse(nums, middle, nums.length-); // reverse right part
reverse(nums, , nums.length-); // reverse whole part
} public void reverse(int[] arr, int s, int e)
{
while(s < e)
{
int temp = arr[s];
arr[s] = arr[e];
arr[e] = temp; s++;
e--;
}
}
}
-----------------------------
方法二:
class Solution {
public:
void rotate(vector<int>& nums, int k) {
//方法一:另设置一个vector,然后逐个元素添加进去,最后将这个vector赋值给nums。添加方式为将右边的k个元素添加进去,再将左边的n-k个元素添加进去。
if (nums.size() == ) return;
if (k > nums.size()) k %= nums.size();
vector<int> newNums;
for (int i = nums.size() - k; i < nums.size(); ++i)
newNums.push_back(nums[i]);
for (int i = ; i < nums.size() - k; ++i)
newNums.push_back(nums[i]);
nums = newNums;
//方法二:自带的rotate函数
/*
int len = nums.size();
if (len > 1) {
k %= len;
std::rotate(nums.begin(), nums.end() - k, nums.end());
}
*/
}
};
【easy】189. Rotate 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 ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【leetcode❤python】 189. Rotate Array
#-*- coding: UTF-8 -*-#由于题目要求不返回任何值,修改原始列表,#因此不能直接将新生成的结果赋值给nums,这样只是将变量指向新的列表,原列表并没有修改.#需要将新生成的结果赋予 ...
- 【Leetcode】【Easy】Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- 448. Find All Numbers Disappeared in an Array【easy】
448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- 1. Two Sum【easy】
1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...
随机推荐
- Django(七)缓存、信号、Form
大纲 一.缓存 1.1.五种缓存配置 1.2配置 2.1.三种应用(全局.视图函数.模板) 2.2 应用多个缓存时生效的优先级 二.信号 1.Django内置信号 2.自定义信号 三.Form 1.初 ...
- vscode的插件收集
转:https://zhuanlan.zhihu.com/p/27905838 转:https://segmentfault.com/a/1190000006697219
- Codeforces 1108F MST Unification(最小生成树性质)
题目链接:MST Unification 题意:给定一张连通的无向带权图.存在给边权加一的操作,求最少操作数,使得最小生成树唯一. 题解:最小生成树在算法导论中有这个性质: 把一个连通无向图的生成树边 ...
- Docke--利用 commit 理解构建镜像
Docker 利用commit理解构建镜像 镜像是容器的基础,每次执行 docker run 的时候都会指定哪个镜像作为容器运行的基础.当我们使用Docker Hub的镜像无法满足我们的需求时,我们就 ...
- vue+elementUI+axios实现的全局loading加载动画
在项目中,很多时候都需要loading加载动画来缓解用户的焦虑等待,比如说,我打开了一个页面,而这个页面有很多接口请求,但浏览器的请求并发数就那么几个,再加上如果网速不行的话,那么这时候,用户很可能就 ...
- Java基础 -- Collection和Iterator接口的实现
Collection是描述所有序列容器(集合)共性的根接口,它可能被认为是一个“附属接口”,即因为要表示其他若干个接口的共性而出现的接口.另外,java.util.AbstractCollection ...
- Digao 连接Mysql 连接不上解决办法
错误一:No module named 'MySQLdb' 原因:python3连接MySQL不能再使用mysqldb,取而代之的是pymysql. 解决方法:在python的MySQL包中,即路径: ...
- CentOS安装glibc-2.14
CentOS安装glibc-2.14 到http://ftp.gnu.org/gnu/glibc/下载glibc-2.14.tar.gz wget https://ftp.gnu.org/gnu/ ...
- Linux基础整理
命令 说明 chsh 查看和修改当前登录的Shell export 查看和设置Shell环境变量 read 读取从键盘或文件输入的数据 expr 四则远算和字符串运算 tmux 一个窗口操作多个会话 ...
- 实现select联动效果,数据从后台获取
效果如下: 当type值选择完后,amount值会自动相应填入. 1. 从后台获取数据,为一个数组,里面包含多个对象. <select id="scholarshipTypeSelec ...