[ARTS] clocking the third week

ARTS completed a week

  • Do at least a week leetcode algorithm problem

  • Read and review at least one technical articles in English

  • At least one technical skill learning

  • Share ideas and think there is a technical article.

  • (That is, Algorithm, Review, Tip, Share short ARTS)

    Algorithm

    11. Sheng most water containers

    Given n non-negative integers a1, a2, ..., an, a point (i, ai) each represents the number of coordinates. Videos n lines in vertical coordinate, i is a vertical line two end points are (i, AI) and (i, 0). Find out the two lines, so that the container together with the x-axis configuration which can accommodate up.

    Solution 1

    Brute force method

    Time complexity: O (n ^ 2)

    Thinking: twice traverse the array, take the product of the length of the smaller value of the difference between the two index i.e. the area to record the maximum area

    int maxArea(vector<int>& height) {
      int var = 0;
      int high = 0;
      int area = 0;
      for (int i = 0; i < height.size(); i ++)
      {
        for (int j = i + 1; j < height.size(); j ++)
        {
          high = height[i] > height[j] ? height[j]:height[i];
          area = high * (j - i);
          if (area > var)
          {
            var = area;
          }
        }
      }
    
      return var;
    }

    Solution 2

    Double pointer

    Time complexity: O (n)

    Thinking: head and tail pointer is moved to the middle, the length of the smaller end of every movement (because the width is gradually reduced, the length of the reference points is no longer large area), the area is calculated, taking the maximum area

    int maxArea(vector<int>& height) {
      int left = 0, right = height.size() - 1;
      int var = 0;
      int high = 0;
      int area = 0;
      while(left < right)
      {
        if (height[left] <= height[right])
        {
          high = height[left];
          left ++;
        }
        else
        {
          high = height[right];
          right --;
        }
    
        area = high * (right - left + 1);
        if (area > var)
        {
          var = area;
        }
      }
      return var;
    }

    Review

    https://tour.golang.org/flowcontrol/1

    • for

    • go only one loop structure, for circulation

      • three parts:

        • init initial statement
          • Variable variable declarations, and statements can only be used for loop statement
        • condition conditional expression
          • When the conditional expression is false, the loop stops
        • post assignment expression
      • } {for loop must always exist

      • init post statement is optional

        • When the init post does not exist, two; can be removed, while loop for loop statement c

          img

      • Infinite loop

        img

    • if

      • The conditional expression does not require (), but needs to execute the statement {}

        img

      • if support variable declaration, the variable can only be used if and else statements execution

        img

    • switch

      img

      • No need to write break after each case, go statement provided automatically break

      • After the switch without conditions if similar if then else statements

        img

    • defer

      • defer a declaration statement is only executed if the function returns where

        img

      • Within a function includes a plurality defer statement, the first statement before execution

        img

    Tip

    iptables forwarding technology

    As the demand for work this week, looked under iptables forwarding relevant knowledge

    Share

    Git branch management practices | Best Practices Code cloud

Guess you like

Origin www.cnblogs.com/JesseTsou/p/11300469.html