In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to solve the problem of leetcode rotation array". 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!
Problem-solving ideas
The violence method rotates one position at a time, and the correct answer is to rotate k times.
When rotating, the precursor node is also used to achieve, and the update of the precursor node is also based on the temp variable.
The focus here is on how to complete a backward move. Don't try to solve problems in the current stage. Violence can be solved by violence.
The code class Solution {public void rotate (int [] nums, int k) {int previous; int temp; for (int I = 0; I < k; iTunes +) {previous = nums [nums. Length-1]; / / the precursor node is initially the last node for (int j = 0; j < nums.length) Temp +) {temp = nums [j]; / / Save the current node first [j] = previous; previous=temp; / / Update precursor node} inversion method is very practical
This method is based on the fact that when we rotate the array k times, the k% n tail elements are moved to the head, and the remaining elements move backward in turn.
1. Inversion can put k% n elements in front first, and you only need to reverse the array in the range of 0 to kmur1 to get the desired order.
2. Reverse the remaining kmur1 to nmur1 elements, which means that the elements move backward in turn.
3. When going to essentialism, the k here may exceed the length of the array, and if it is exactly equal to the length of the array, it is equal to the same k=k%n.
Class Solution {public void rotate (int [] nums, int k) {int n = nums.length; k% = n; / / k may check array length to cause error / / 1, reverse array reverse (nums,0,n-1) / / 2. The first k reverses, and the last nMurk reverses reverse (nums,0,k-1); reverse (nums,k,n-1) } / / inverted arrays can be easily used to reverse arrays of any interval using public void reverse (int [] nums, int start, int end) {while (start < end) {int temp = nums [start]; nums [start] = nums [end]; nums [end] = temp; start++; end-- Ring replacement
Yes, but when the code is implemented, it encounters a dead loop when n%k==0 and can't think of a solution.
The following is the code provided by leetcode, skillfully using a double loop to solve the embarrassment that I can't solve, the core code is all the same, and the outer loop must be the length of the array.
I didn't expect to use count to control the number of external loops and inner loops in the end. There is still a great improvement in coding.
Public class Solution {public void rotate (int [] nums, int k) {k = k% nums.length; int count = 0; for (int start = 0; count < nums.length; start++) {int current = start; int prev = nums [start]; do {int next = (current + k)% nums.length Int temp = nums [next]; nums [next] = prev; prev = temp; current = next; count++;} while (start! = current);} "how to solve the leetcode rotation array problem" 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.