Surface preparation by - Kuang view

A. Full array of characters

   A full array of the array, no repetitive elements, defined as a recursive function i-1 before the element has been arranged in full lined up, the i-th element and subsequent full array elements. Process for the i-th element of the last element to turn on the i-th position, then element i + 1-th and subsequent full array elements.

class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> res;
        permutehelper(nums, 0, res);
        return res;
    }

private:
    void permutehelper(vector<int>&nums, int l, vector<vector<int>> &res){
        if (l == nums.size()) {
            res.push_back(nums);}       
       for (int i = l; i < nums.size(); i++) {
                swap(nums[l], nums[i]);
                permutehelper(nums, l+1, res);
                swap(nums[i], nums[l]);
            }
   
    }  

};

 

II. Large integer multiplication leetcode43 MultiStrings

  Large integer multiplication in two steps, a first step the first large integer multiplies each bit of the second bit is large. Note back to 0s, the second step in the first step a large number of addition of intermediate results.

 The first trick. . Finally, when implemented with a unitary binary vector <int> and translated into strings.

class Solution {
public:
    string multiply(string num1, string num2) {
        if (num1 == "0" || num2 == "0") return "0";
        int len_1 = num1.size();
        int len_2 = num2.size();
        vector<string> holdMul;
        for (int i=0; i<len_2; i++){
            string tmp= "";
            tmp = num2[len_2 - 1 -i];
            string fillZero(i, '0');
            holdMul.push_back(mulOnebit(num1, tmp) + fillZero);
        } 
        string sum=holdMul[0];
        for  (int j=1; j< holdMul.size(); j++) {
            sum = addStr(sum, holdMul[j]);
            
        }
        return sum;      
    }
    
private:  
    string addStr(string a, string b) {
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());
        int i = 0;
        int j = 0;
        vector<int> result;
        while(i< a.size() && j < b.size()) {
           result.push_back((a[i++] - '0')  +  (b[j++] - '0'));         
        }
        while(i < a.size()) result.push_back(a[i++] - '0');
        while(j < b.size()) result.push_back(b[j++] - '0');
        string re =  convertNumber(result);
        return re;
        
    }
    string mulOnebit(string a, string b) {
        vector<int> result;
        for(int i=a.size() -1; i >= 0; i--){
            result.push_back((a[i] - '0') * (b[0] - '0'));
            cout << result.back()<< " ";
        }
        string re = convertNumber(result);
        return re;
        
    }
    string convertNumber(vector<int> input) {

        int len = input.size();
        for(int i = 0; i < len; i++) {           
            if (input[i] >= 10) {
                if (i < len - 1){
                input[i + 1] += (input[i] / 10); 
                }
                else {
                   input.push_back(input[i] / 10);
                }
                input[i] %= 10;
            }           
        }

        reverse(input.begin(), input.end());
        string result(input.size(), '0');
        for (int i=0; i< result.size(); i++) {
            result[i] += input[i];
        }
            
        return result;
        
    }
    
};

 

III. How to efficiently complete binary tree insertion

https://blog.csdn.net/fangjian1204/article/details/39179343

https://www.cnblogs.com/qieerbushejinshikelou/p/3917019.html

(1) O (n) in solution using a queue level binary tree traversal, taken out of the rightmost node insertion

  (2)   O (logN * logN) for Solving recursively. Judgment about the height of the sub-tree, if h is larger than the left subtree right subtree h, then the left subtree inside to find, or find the right sub-tree inside

TreeNode* getLastNode(TreeNode* root) {
           If (root == NULL || root ->left == NULL)  return root;
            int lh = 0;
            TreeNode* p = root;
            while(p) {
               lh++;
               p = p-> left;
          }
            Int rh = 0
             p = root;
             while(p) {
               rh++;
               p = p->right;
           }

           If (lh > rh) return getLastNode(root->left);
           else return getLastNode(root->right);
}

(3) O (logn) method

 First statistical depth of the tree, began to search the right sub-tree, has been found to the left to the leaf node,

 If the depth of the right subtree <whole tree deep, then back to the left sub-tree search

Otherwise, if found to leaf node corresponds to the root has no right child, the leaf nodes return.

        If the search to the leaf node corresponding to the root there with children, the search for the right sub-tree.

TreeNode* getLastNode(TreeNode* root) {

          If (root ==NULL || root->left == NULL) return root;
          int height =0;
          TreeNode*p = root;
          while(p) {
              height++;
              p = p->next; 
         }
         int level = 0;
         while(root) {
                 level++;
                 If (level == height) break;  // 右子树为空的情况。
                If (root->right) {
                Int tmp_len = level;
                 p = root;
                TreeNode *leaf = root->right;
                      while(leaf) {
                             tmp_len++;
                             p = leaf;
                            leaf = leaf ->left;
                      }
                     If (tmp_len < height)  root = root->left;
                     else if (p->right == NULL || p->right == leaf) return leaf;
                     else root = root->right;

              }
              else root = root->left;
             
         }
         return root;
}

 

IV. Design LRU

 

V. elect a maximum of k digits in the hundreds of millions size of the array

 

VI. A single list sorted leetcode 146

class Solution {
public:// 归并排序 找中点
    ListNode* sortList(ListNode* head) {
        if(head == NULL || head->next == NULL) return head;
        ListNode* slow = head;
        ListNode* fast = head;
        while(fast->next && fast->next->next) {
            fast = fast->next->next;
            slow = slow->next;
        }
        
        fast = slow->next;
        if(slow->next)
        slow->next = NULL;
        ListNode* l1 = sortList(head);
        ListNode* l2 = sortList(fast);
        return merge(l1, l2);
    }
private: 
    ListNode* merge(ListNode* p1, ListNode* p2) {
        ListNode* p0 = new ListNode(0);
        if(p1 == NULL) return p2;
        if(p2 == NULL) return p1;
        ListNode* p = p0;
        while(p1 || p2) {
            int temp1 = (p1 == NULL ? INT_MAX : p1->val);
            int temp2 = (p2 == NULL ? INT_MAX : p2->val);
            if(temp1 <= temp2) {
                p->next = p1;
                p1 = p1->next;
                p = p->next;
            } else {
                p->next = p2;
                p2 = p2->next;
                p = p->next;
            }
        }
        /*if(p1){
            p->next = p1;
        }
        if(p2){
            p->next = p2;
        }*/
        return p0->next;
    }
};

VII. Merging two sorted linked list

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if (l1 == NULL) return l2;
        if (l2 == NULL) return l1;
        ListNode * head = new ListNode(0);
        ListNode * p = head;
        while (l1 != NULL && l2 != NULL) {
            if (l1->val <= l2->val) {
                p->next = l1;
                l1 = l1->next;
            }
            else {
                p->next = l2;
                l2 = l2->next;
            }
            p = p->next;
        }
        if (l1) {
            p->next = l1;
        }
        if (l2) {
            p->next = l2;
        }
        ListNode * tmp = head->next;
        delete head;
        return tmp;
    }
};

 

VIII. Frog jump, the peripheral elements of the output matrix, a large array number for the first time to appear

 

VIII. Binary conversion algorithm of the preamble, the order and sequence after sequence, there are limitations to achieve

 

IX. The shortest path

 

X. demining two rows, the second row is given, determining the first row is not a mine

 

XI. The most common sub-tree into O (h)

 

XII. One-dimensional max_pooling

 

 

XIII. Generator matrix

 

Guess you like

Origin www.cnblogs.com/cookcoder-mr/p/11129707.html