题目标签: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. Unity TimeLine

    最近一直再看这方面的内容,看的比较多知识点比较分散,所以目的就是把这些知识点内容梳理一边,并作记录. PlayableDirector与TrackAsset,TrackAsset与PlayableAs ...

  2. Python selenium —— 一定要会用selenium的等待,三种等待方式解读

    发现太多人不会用等待了,博主今天实在是忍不住要给大家讲讲等待的必要性. 很多人在群里问,这个下拉框定位不到.那个弹出框定位不到…各种定位不到,其实大多数情况下就是两种问题:1 有frame,2 没有加 ...

  3. 数据库常用的事务隔离级别和原理?&&mysql-Innodb事务隔离级别-repeatable read详解

    转载地址:https://baijiahao.baidu.com/s?id=1611918898724887602&wfr=spider&for=pc https://blog.csd ...

  4. ClientValidationEnabled

    ClientValidationEnabled 去掉这个的时候就无效了,前端验证 <add key="ClientValidationEnabled" value=" ...

  5. html2canvas 识别 svg 解决方案

    参考: 预览地址 http://jsfiddle.net/bv16o50f/1/ html <div class="visualization"> <svg xm ...

  6. 机器学习---感知机(Machine Learning Perceptron)

    感知机(perceptron)是一种线性分类模型,通常用于二分类问题.感知机由Rosenblatt在1957年提出,是神经网络和支持向量机的基础.通过修改损失函数,它可以发展成支持向量机:通过多层堆叠 ...

  7. magento 2.2.3 -/.gitignore -/.htaccess 分享

    /.htaccess ############################################ ## overrides deployment configuration mode v ...

  8. C# 将前端传来的图片文件分别以大图和缩略图保存

    HttpPostedFile pic_upload = Request.Files["file"]; Bitmap bitmap = (Bitmap)System.Drawing. ...

  9. Scrum【转】

    转载自:https://www.cnblogs.com/l2rf/p/5783726.html 灵感来自于一段冷笑话: 一天,一头猪和一只鸡在路上散步,鸡看了一下猪说,“嗨,我们合伙开一家餐馆怎么样? ...

  10. python2 配置环境变量

     复习 '''重点:1.进制转换:二进制 与 十六进制2.内存分布:栈区 与 堆区 # 124810101001110111 => 2a77abf1 => 1010101111110001 ...