160 Race Week: Array conversion

Topics are as follows:

  

First of all, give you an initial array  arr. Then, every day you have to generate a new array of arrays based on the previous day.

Of  i the generated array days, is to you first  i-1 day of the array obtained as follows:

  1. If an element is less than its left and right neighbors, then the self-energizing elements  1.
  2. If an element is greater than its left and right neighbors, then the element is decremented  1.
  3. The first and last elements never change.

After a while, you'll find an array will not change, return an array finally obtained.

Sample the following

In fact, this is a simple question of violence, an infinite loop, a for loop, three judge sentence, a conditional statement out of infinite loop, finished.

  Since we do not know the day is the time to change the array, so I write an infinite loop.

  Next it is for, the title update to the array of ways:

    1, than the adjacent big is decremented

    2, than the adjacent small to plus

    3, head and tail nodes unchanged

  Obviously we need to iterate over the array from start to finish, so set the layer for

  Is the three determination conditions and update conditions echoes in the interior of the loop for

    1、if (!i || i == n - 1)

    2、if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1])

    3、if (arr[i] < arr[i - 1] && arr[i] < arr[i + 1])

  Fill in the corresponding code in three years if statement, you can complete array update. Oh, yes, there is an array of one else does not meet the above implementation else, and conditional statements 1 must be placed in front of most of the for loop body.

  

At the end of the for loop, we have to determine how to jump out of the cycle of death, subject to remember what we export it, the same array, that is, when the temp == arr, you can break up, otherwise arr = temp; so the entire code framework came out. Below posted about c ++ code to achieve:

  

 1 class Solution {
 2 public:
 3     vector<int> transformArray(vector<int>& arr) {
 4         int len = arr.size();
 5         vector<int> temp;
 6         temp.resize(len);
 7         
 8         while (1)
 9         {
10             for (int i = 0; i < len; i++)
11             {
12                 if (0 == i || i == len -1)
13                 {
14                     temp[i] = arr[i];
15                     continue;
16                 }
17                 if (arr[i] > arr[i-1] && arr[i] > arr[i+1])
18                 {
19                     temp[i] = arr[i]-1;
20                     continue;
21                 }
22                 if (arr[i] < arr[i-1] && arr[i] < arr[i+1])
23                 {
24                     temp[i] = arr[i]+1;
25                     continue;
26                 }
27                 temp[i] = arr[i];
28             }
29             if (temp == arr)
30             {
31                 break;
32             }
33             arr = temp;
34         }
35         
36         return temp;
37     }
38 };

  Race week is not easy, gentlemen encourage each other!

Guess you like

Origin www.cnblogs.com/daker-code/p/11784994.html