【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 ...
随机推荐
- wince可用的7-zip
7-zip下载 7-zip
- C# call webservice方法
https://www.cnblogs.com/Fooo/p/5507153.html
- 【12】Django 中间件
前戏 我们在前面的课程中已经学会了给视图函数加装饰器来判断是用户是否登录,把没有登录的用户请求跳转到登录页面.我们通过给几个特定视图函数加装饰器实现了这个需求.但是以后添加的视图函数可能也需要加上装 ...
- Mac OSX下安装dlib (Python)
1.在安装Dlib库之前需要安装的库:opencv,numpy 2.安装Xquartz2.7.11 Xquartz是执行Unix程序的图形窗口环境,为了兼容Unix和Linux下移植过来的程序就需要安 ...
- 用video标签流式加载
video标签 浏览器的video标签通常是接收一个src属性,然后浏览器就会根据这个src属性来自动加载视频.这个过程是浏览器来加载video的. 这种方式有什么问题吗? mp4文件不能流式加载 w ...
- ES6中6种声明变量的方法
相关阅读:http://es6.ruanyifeng.com/#docs/let 相关阅读:https://www.cnblogs.com/ksl666/p/5944718.html 相关阅读:htt ...
- [BJOI2019]删数(线段树)
[BJOI2019]删数(线段树) 题面 洛谷 题解 按照值域我们把每个数的出现次数画成一根根的柱子,然后把柱子向左推导,\([1,n]\)中未被覆盖的区间长度就是答案. 于是问题变成了单点修改值,即 ...
- 20165223《网络对抗技术》Exp4 恶意代码分析
目录 -- 恶意代码分析 恶意代码分析说明 实验任务目标 实验内容概述 schtasks命令使用 实验内容 系统运行监控 恶意软件分析 静态分析 virscan分析和VirusTotal分析 PEiD ...
- GWAS研究中case和control的比例是有讲究的?
GWAS研究中,表型分两种.第一种是线性的表型,如果身高.体重.智力等:第二种是二元的表型,比如患病和未患病,即通常所说的case和control.对于表型是线性的样本来说,是不存在case和cont ...
- maven deploy 指定-DaltDeploymentRepository
运行deploy出现如下错误: deployment failed repository element was not specified in the POM inside distributio ...