Leetcode summary posts [11-20]

11. Container with Most Water

Double pointer i and j, respectively, directed at both ends of the array. Each time the maximum value is res = max (res, (j - i) * min (height [j], height [i])). Each comparison height [i] and the size of the height [j] is updated in accordance with i and j.

Only at a height [i] or height [j] is greater than the current maximum before considering the next update.

12. Integer to Roman

This question can only be hard to do, be a realization problem

 1 string intToRoman(int num) {
 2         string res = "";
 3         vector<int> val{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
 4         vector<string> str{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
 5         for (int i = 0; i < val.size(); ++i) {
 6             while (num >= val[i]) {
 7                 num -= val[i];
 8                 res += str[i];
 9             }
10         }
11         return res;
12 }

13. Roman to Integer

Ditto

 1  int romanToInt(string s) {
 2         int res = 0;
 3         unordered_map<char, int> m{{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
 4         for (int i = 0; i < s.size(); ++i) {
 5             int val = m[s[i]];
 6             if (i == s.size() - 1 || m[s[i+1]] <= m[s[i]]) res += val;
 7             else res -= val;
 8         }
 9         return res;
10 }

14. Longest Common Prefix

If it is common prefix then each string will have, as long as the first one to do than to take on it. Two pointers i: a current share of the char of the prefix index, j: index pointing to a back of a string

15. 3Sum

Use double pointer to find the current element complement, but the premise is to use an array to be sorted .

while (current int)

  while (left < right)

    find sum such that [left] + [right] == target - int

    if sum > target then right--

    else left++

Details of the process: the same element is skipped, because the need to return to the last embodiment is composed of all

16. 3Sum Closest

Still use double pointer, but the addition of a new variable diff

while (current int)

  while (left < right)

    diff = min(diff, target - current int - [left] + [right])

    if sum > targe then right--

    else left++

return diff

17. Letter Combination of a Phone Number

First, the use of each digital memory map may correspond to letters and dfs each iteration of the current node state

dfs function: dfs (current index of 12, current node)

18. 4Sum

Similarly 3Sum, just to add a layer of traversing the second element in the periphery, and finally continue to use the two-pointer to find the current complement of two elements, but the premise is to use an array to be sorted .

while (current first int)

  while (current second int)

    while (left < right)

      find sum such that [left] + [right] + first + second== target - int

      if sum > target then right--

      else left++

19. Remove Nth node from the end of list

Because a single pointer, so in order to remove the need to find the next current and previous element, and the two can be connected.

Using the pointer to find the speed on the n th element of the current node. Some fast than slow fast node n nodes, then the node when the slow speed of the tail reaches the node happens to be the n-th node, this time to slow down at a node connected to the node can be slow.

20. Valid Parentheses

Using the stack to store the current character, use the map store opening -> closing char.

As long as the key (is opening char) then push the stack in

else is determined whether the stack is empty or top current and the current does not correspond to char, return false

The remaining cases illustrate the current char and on a match, then a can directly pop out

Finally, if the stack is empty That explanation is valid

stack only keep the last match of the opening char has not yet been

 

Guess you like

Origin www.cnblogs.com/goldenticket/p/12244224.html