【LeetCode】Rotate Array

Rotate Array

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.

Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.

AC代码:(Python)

1 class Solution:
2     # @param nums, a list of integer
3     # @param k, num of steps
4     # @return nothing, please modify the nums list in-place.
5     def rotate(self, nums, k):
6         n = len(nums)
7         k = k % n
8         nums[:] = nums[n-k:] + nums[:n-k]
9         

要注意一个问题:

A little important thing to be cautious:

nums[:] = nums[n-k:] + nums[:n-k]

can‘t be written as:

nums = nums[n-k:] + nums[:n-k]

on the OJ.

The previous one can truly change the value of old nums, but the following one just changes its reference to a new nums not the value of old nums.

因为题目要求的是:

@return nothing, please modify the nums list in-place.
时间: 2024-10-09 06:46:53

【LeetCode】Rotate Array的相关文章

【Leetcode】Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 思路:第一种思路是一层一层的进行旋转,比较直观:第二种思路则比较取巧,首先沿着副对角线翻转一次,然后沿着水平中线翻转一次. 代码一: class Solution { public: void rotate(vector<

【LeetCode】Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. start with an example. Given [0,1,2], rotate 1 steps to the right -&

【LeetCode】 Rotate List 循环链表

题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a list, rotate the list to the right by k places, where k is non-negative. * 题目:循环移动链表,等价于将链表从右边数的k个节点移动到表的前方 * 思路:移动倒是简单,重点是要找到链表倒数的k个数,就等价于找到倒数第K+1个数,设置两个指针,

【leetcode】Rotate Image(middle)

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 思路:我的思路,先沿对角线对称,再左右对称. void rotate(int **matrix, int n) { //先沿对角线对称 for(int i = 0; i < n; i++) { for(int j = 0;

【leetcode】Rotate List(middle)

Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL. 题目关键点:k可能比链表长度还大, 需要获取链表长度len, 用 k % len 获取具体需要移动的数字. class Solution

【Leetcode】Patching Array

题目链接: 题目: Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n] inclusive can be formed by the sum of some elements in the array. Return the minimum number of patches require

【LeetCode】9.Array and String — Longest Common Prefix 最长共同前缀

Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: ["flower","flow","flight"] Output: "fl" Exa

【LeetCode】11.Array and String —Array Partition I 数组分区

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible. Example 1: Input: [1,4,3,2] Output: 4 Explan

【LeetCode】10.Array and String —Reverse String 字符数组逆置

Write a function that reverses a string. The input string is given as an array of characters char[]. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. You may assume all the