在一片广阔的草原上,有许多分布在不同位置的牛群。牧人希望通过选择一个合适的值 k ,使得每一个牛群向左移动 k 个位置,以方便管理。给定一个整数数组 nums,表示每个牛群的位置,请编写一个函数,实现将数组元素向左轮转 k 个位置,其中 k 是非负数。
示例1
输入
[1, 2, 3, 4, 5, 6, 7],3
输出
[4,5,6,7,1,2,3]
备注:
注意:你可以尝试编写空间复杂度为 O(1) 的算法。 nums(1 k (0 1
加载中...
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型ArrayList */ public ArrayList
rotate_left (int[] nums, int k) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @param k int整型 * @return int整型vector */ vector
rotate_left(vector
& nums, int k) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @param k int整型 # @return int整型一维数组 # class Solution: def rotate_left(self , nums , k ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ public List
rotate_left (List
nums, int k) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ function rotate_left( nums , k ) { // write code here } module.exports = { rotate_left : rotate_left };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @param k int整型 # @return int整型一维数组 # class Solution: def rotate_left(self , nums: List[int], k: int) -> List[int]: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ func rotate_left( nums []int , k int ) []int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param numsLen int nums数组长度 * @param k int整型 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* rotate_left(int* nums, int numsLen, int k, int* returnSize ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @param k int整型 # @return int整型一维数组 # class Solution def rotate_left(nums, k) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ def rotate_left(nums: Array[Int],k: Int): Array[Int] = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ fun rotate_left(nums: IntArray,k: Int): IntArray { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ public int[] rotate_left (int[] nums, int k) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ export function rotate_left(nums: number[], k: number): number[] { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param k int整型 * @return int整型一维数组 */ func rotate_left ( _ 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 rotate_left(&self, nums: Vec
, k: i32) -> Vec
{ // write code here } }
[1, 2, 3, 4, 5, 6, 7],3
[4,5,6,7,1,2,3]