农场主人有一群牛,他给每只牛都打了一个编号,编号由整数表示。这些牛按照编号的顺序形成了一个队列。现在农场主人想将队列中的牛向左轮转 k 个位置,你能帮他设计一个算法来实现这个功能吗?注意,这里的轮转是指将队列的第一个元素移动到队列的末尾,进行 k 次。要求原地操作,不要开辟额外的空间。
示例1
输入
[1,2,3,4,5,6,7],3
输出
[5,6,7,1,2,3,4]
示例2
输入
[-1,-100,3,99],2
输出
[3,99,-1,-100]
备注:
0 数组长度n,1
加载中...
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ public int[] rotateCows (int[] nums, int k) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @param k int整型 * @return int整型vector */ vector
rotateCows(vector
& nums, int k) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @param k int整型 # @return int整型一维数组 # class Solution: def rotateCows(self , nums , k ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ public List
rotateCows (List
nums, int k) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ function rotateCows( nums , k ) { // write code here } module.exports = { rotateCows : rotateCows };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @param k int整型 # @return int整型一维数组 # class Solution: def rotateCows(self , nums: List[int], k: int) -> List[int]: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ func rotateCows( nums []int , k int ) []int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param numsLen int nums数组长度 * @param k int整型 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* rotateCows(int* nums, int numsLen, int k, int* returnSize ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @param k int整型 # @return int整型一维数组 # class Solution def rotateCows(nums, k) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ def rotateCows(nums: Array[Int],k: Int): Array[Int] = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ fun rotateCows(nums: IntArray,k: Int): IntArray { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ public int[] rotateCows (int[] nums, int k) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ export function rotateCows(nums: number[], k: number): number[] { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ func rotateCows ( _ nums: [Int], _ k: Int) -> [Int] { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ pub fn rotateCows(&self, nums: Vec
, k: i32) -> Vec
{ // write code here } }
[1,2,3,4,5,6,7],3
[5,6,7,1,2,3,4]
[-1,-100,3,99],2
[3,99,-1,-100]