In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to rotate the array of JavaScript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. What is a rotation array
The rotation array is divided into two categories: left rotation and right rotation. Force buckle 189 is entitled right rotation, and what is shared today is left rotation.
Given an array, rotate the elements in the array k positions to the left, where k is a non-negative number.
two。 Annular substitution
Ring substitution is difficult to understand. If you put the data of the array on the regular polygon and replace the data with the idea of checkers, it would be easy to draw a picture.
If Num5, K2, and the numbers 1-5 are placed on the vertices of the Pentagon in turn, the trajectory of the digital replacement is 1-3-5-2-4-1, back to the origin, count = n, it ends. The trajectory happens to be a five-pointed star.
If the numbers 6, K2 and the numbers 1-6 are placed at the top of the hexagon in turn, the trajectory of the number replacement is 1-3-5-1, back to the origin, count.
< n, start++, 接着 2-4-6-2,回到原点,count = n, 结束。轨迹是六边形的2个内嵌正三角形。 其它多边形类似,隔k个点往前走,总能走回原点,如果中间有漏的,旋转一个角的方位重复进行上述步骤就能走完所有的顶点了。 3.LeetCode题目 给你一个数组,将数组中的元素向右轮转 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]4.解法 我们用下面的例子更具体地说明这个过程: nums = [1, 2, 3, 4, 5, 6]k = 2 5.代码实现 leetcode超时,但是思路对,删除最后一个,把最后删除的那个元素添加到数组的最前面 for (var i = 0; i < k; i++) { var p = nums.pop(); nums.unshift(p); } return nums; 这个是又更改算法了 var rotate = function(nums, k) { if (k >0) {var arr = []; for (var I = 0; I < nums.length; I +) {arr [I] = nums [I];} for (var I = 0; I < nums.length; I +) {nums [(I + k)% arr.length] = arr [I] } return nums;}} console.log (rotate ([1, 2, 3, 4, 5, 6, 7], 3)); "how to rotate the array JavaScript" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.