网站为什么维护中,公司网站建设属于软件销售,房产中介如何做网站,网站建设与维护一般需要多少钱每年[leetcode 189][轮转数组]
给定一个整数数组 nums#xff0c;将数组中的元素向右轮转 k 个位置#xff0c;其中 k 是非负数。 示例 1: 输入: nums [1,2,3,4,5,6,7], k 3 输出: [5,6,7,1,2,3,4] 解释: 向右轮转 1 步: [7,1,2,3,4,5,6] 向右轮转 2 步: [6,7,1,2,3,4,5] 向右…[leetcode 189][轮转数组]
给定一个整数数组 nums将数组中的元素向右轮转 k 个位置其中 k 是非负数。 示例 1: 输入: nums [1,2,3,4,5,6,7], k 3 输出: [5,6,7,1,2,3,4] 解释: 向右轮转 1 步: [7,1,2,3,4,5,6] 向右轮转 2 步: [6,7,1,2,3,4,5] 向右轮转 3 步: [5,6,7,1,2,3,4] 示例 2: 输入nums [-1,-100,3,99], k 2 输出[3,99,-1,-100] 解释: 向右轮转 1 步: [99,-1,-100,3] 向右轮转 2 步: [3,99,-1,-100] class Solution {public static void rotate(int[] nums, int k) {int n nums.length;if (n 1) {return;}int index k % n;reverse(nums, 0 , n - 1);reverse(nums, 0, index - 1);reverse(nums, index, n - 1);}public static void reverse(int[] nums, int start, int end) {while (end start) {int temp nums[start];nums[start] nums[end];nums[end] temp;start;end--;}}
}