Crazy LC first day

Foreword: In order to find a summer internship, try to complete 200 questions as soon as possible! !

Question 46. Full arrangement (medium)

Method: backtracking method; depth first traversal (DFS)

topic:

class Solution {
public:
    void backtrack(vector<vector<int>>& res, vector<int>& output, int first, int len){
        // 所有数都填完了
        if (first == len) {
            res.emplace_back(output);
            return;
        }
        for (int i = first; i < len; ++i) {
            // 动态维护数组
            swap(output[i], output[first]);
            // 继续递归填下一个数
            backtrack(res, output, first + 1, len);
            // 撤销操作
            swap(output[i], output[first]);
        }
    }
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int> > res;
        backtrack(res, nums, 0, (int)nums.size());
        return res;
    }
};

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/permutations/solution/quan-pai-lie-by-leetcode-solution-2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Complexity analysis:

  • Time complexity: O(n×n!), where nn is the length of the sequence.
  • Space complexity: O(n)O(n), where nn is the length of the sequence. In addition to the answer array, the recursive function needs to allocate stack space for each layer of recursive function during the recursion process, so extra space is needed here and the space depends on the depth of the recursion. Here we can see that the recursive call depth is O(n)O(n )

C++ knowledge points:

Vector is our commonly used container, and the commonly used methods for adding elements to it are: emplace_back and push_back.

  • push_bach():

First, you need to call the constructor to construct a temporary object, then call the copy constructor to put the temporary object in the container, and then release the temporary variable.

  • emplace_back():

This element is constructed in place, without triggering copy construction and transfer construction.

It is recommended to use emplace_back()



Question 47 Full Sort Ⅱ

Method: Backtracking

topic:

class Solution {
    vector<int> vis;

public:
    void backtrack(vector<int>& nums, vector<vector<int>>& ans, int idx, vector<int>& perm) {
        if (idx == nums.size()) {
            ans.emplace_back(perm);
            return;
        }
        for (int i = 0; i < (int)nums.size(); ++i) {
            if (vis[i] || (i > 0 && nums[i] == nums[i - 1] && !vis[i - 1])) {
                continue;
            }
            perm.emplace_back(nums[i]);
            vis[i] = 1;
            backtrack(nums, ans, idx + 1, perm);
            vis[i] = 0;
            perm.pop_back();
        }
    }

    vector<vector<int>> permuteUnique(vector<int>& nums) {
        vector<vector<int>> ans;
        vector<int> perm;
        vis.resize(nums.size());
        sort(nums.begin(), nums.end());
        backtrack(nums, ans, 0, perm);
        return ans;
    }
};

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/permutations-ii/solution/quan-pai-lie-ii-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

To be honest, this question was vomiting for the Lord. I thought I understood it, but if there is an additional condition here, it won't be possible.

Here is a trick, refill after sorting. See it again when you have time, it's really hard to top.



Question 20 Ordered parentheses

Method: Stack

topic:

class Solution {
public:
    bool isValid(string s) {
    
    stack<char> ans;
    for(int i=0;i<s.length();++i)
    {
         if(ans.size()==0 )
         {
         if(s[i]==')' || s[i]==']' || s[i]=='}' )
           return false;
         }
        if(s[i]=='(' || s[i]=='[' || s[i]=='{')
        {
            ans.push(s[i]);
        }
        if(s[i]==')')
        {
            if(ans.top()=='(')
            {ans.pop();}
            else
            {return false;}
        }
        if( s[i]==']' )
        {
             if(ans.top()=='[')
            {ans.pop();}
            else
            {return false;}
        }
        if( s[i]=='}')
        {
             if(ans.top()=='{')
            {ans.pop();}
            else
            {return false;}
        }
    }
    if(ans.size()==0)
    { return true;}
    else
    {return false;}

    }
};

C++:

Pay attention to the difference between single quotation marks and double quotation marks. Use double quotation marks to report an error at the beginning!

Single quotation marks are character type, double quotation marks are string type

For example:

The difference between "a" and'a', the former is a character string, and the latter is a character.
Actually "a" is "a\0" and ends with'\0'. And'a' simply means the character a.
The string can be a combination of multiple characters like "abcde", but'abcde' is wrong! !



 

Sword Finger Offer 24 Questions Reversal Linked List

  • Two solutions: iteration and recursion!
  • Iteration uses double pointers. Recursion needs to be deduced step by step, so understand!  


 Question 15 Sum of three numbers

Method: Sorting + double pointer is the key to removing duplicate solutions (same as the previous trick, first sorting and then skipping the same as the previous one)

Too tired today, find time to write it myself!

Guess you like

Origin blog.csdn.net/weixin_44747789/article/details/114121697