题目标签: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的更多相关文章

  1. 【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  ...

  2. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  3. 【leetcode❤python】 189. Rotate Array

    #-*- coding: UTF-8 -*-#由于题目要求不返回任何值,修改原始列表,#因此不能直接将新生成的结果赋值给nums,这样只是将变量指向新的列表,原列表并没有修改.#需要将新生成的结果赋予 ...

  4. 【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 ...

  5. 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 ...

  6. 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 ...

  7. 561. Array Partition I【easy】

    561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...

  8. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  9. 1. Two Sum【easy】

    1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...

随机推荐

  1. VisualStudio 快捷键

    ctrl + o : 打开当前文件所在文件目录 ctrl + 鼠标左键 : 转到方法或者字段定义

  2. python_while

    while 格式 while 条件 : pass 使用 while True : print("精忠报国") print("粉红的回忆") print(&quo ...

  3. 通过后缀名和MIME-TYPE检查实现文件类型校验

    前言 文件上传是一个在开发中很常见的需求场景,通常出于安全考虑,我们会对上传的文件进行类型校验,其中常见的有后缀名校验,mime-type校验 话不多说,直接上代码 1.首先定义允许上传的文件类型白名 ...

  4. pstree:command not found

    centos7默认并没有安装pstree,所以会有pstree:command not found yum -y install psmisc

  5. easyui判断下拉列表

    {field:'state',title:'状态',width:100, formatter : function(value, row, index){ if (value == 0) { retu ...

  6. Spring Mybatis多数据源配置范例

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. PL2303HX在Windows 10下面不装安装驱动的解决办法(Code:10)

    Prolific在很早之前推出了一款名为PL2303HX的芯片, 用于USB转RS232, 这款芯片使用的范围非常广, 并且年代久远. 但是这款芯片因为用的特别多, 所以中国就有很多厂家生产了仿造的P ...

  8. Conda常见命令

    Anaconda,Miniconda,Conda,Pip的区别: Anaconda:用于科学计算的python发行版,里面预装好了conda,某个版本的python,众多packages,科学计算工具 ...

  9. Java内存溢出和内存泄露后怎么解决

    1.首先这里先说一下内存溢出和内存泄露的区别: 内存溢出 out of memory,是指程序在申请内存时,没有足够的内存空间供其使用,出现out of memory:比如申请了一个integer,但 ...

  10. HDU 1757 A Simple Math Problem (矩阵快速幂)

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...