算法题合集(细分知识点附链接)---------第二部分【融合牛客及力扣】

算法题合集

    • 137. 只出现一次的数字 II
    • 260. 只出现一次的数字 III
    • JZ39 数组中出现次数超过一半的数字
    • 606. 根据二叉树创建字符串
    • 102. 二叉树的层序遍历
    • 236. 二叉树的最近公共祖先
    • JZ36 二叉搜索树与双向链表
    • 105. 从前序与中序遍历序列构造二叉树
  • 动态规划
    • JZ42 连续子数组的最大和

137. 只出现一次的数字 II

https://leetcode.cn/problems/single-number-ii/description/

class Solution {
public:
    int singleNumber(vector<int>& nums) {
       unordered_map<int,int> map;
       for(int num:nums){
           ++map[num];
       }
       int data=0;
       for(auto [num,number]:map){
           if(number == 1){
               data=num;
               break;
           }
       }
    return data;
    }
};

260. 只出现一次的数字 III

https://leetcode.cn/problems/single-number-iii/

class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        unordered_map<int,int> map;
        for(auto num:nums){
            ++map[num];
        }
        vector<int> res;
        for(auto [num,number]:map){
            if(number==1){
                res.push_back(num);
            }
        }
        return res;
    }
};

JZ39 数组中出现次数超过一半的数字

https://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163?tpId=13&&tqId=11181&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking

#include 
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param numbers int整型vector 
     * @return int整型
     */
    int MoreThanHalfNum_Solution(vector<int>& numbers) {
        unordered_map<int, int> map;
        if(numbers.size()==1)
        return numbers[0];

        for(int number:numbers){
            ++map[number];
            for(auto [number,num]:map){
                if(map[number]> (numbers.size()/2))
                    return number;
            }
        }
        return -1;
    }
};

606. 根据二叉树创建字符串

https://leetcode.cn/problems/construct-string-from-binary-tree/description/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    string tree2str(TreeNode* root) {
        if(root==nullptr)
        return "";
        string str=to_string(root->val);
        if(root->left||root->right){
            str+='(';
            str+=tree2str(root->left);
            str+=')';
        }
        if(root->right){
            str+='(';
            str+=tree2str(root->right);
            str+=')';
        }
        return str;
    }
};

102. 二叉树的层序遍历

https://leetcode.cn/problems/binary-tree-level-order-traversal/description/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode*> q;
        size_t size=0;//这个size记录的是每一层的结点个数,这样能使每一层的每个节点都能遍历到去访问左孩子和右孩子
        if(root){
            q.push(root);
            size=1;
        }
        vector<vector<int>> vv;
        while(!q.empty()){
                vector<int> v;
            while(size--){
                TreeNode* front=q.front();
                q.pop();
                v.push_back(front->val);
                if(front->left!=nullptr){
                    q.push(front->left);
                }
                if(front->right!=nullptr){
                    q.push(front->right);
                }
            }
            vv.push_back(v);
            size=q.size();
        }
        return vv;
    }
};

236. 二叉树的最近公共祖先

https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool getPath(TreeNode* root,int data,stack<TreeNode*>& path){
        if(root==nullptr)
        return false;
        path.push(root);
        if(root->val==data)
        return true;
        if(getPath(root->left,data,path))
        return true;
        if(getPath(root->right,data,path))
        return true;
        path.pop();
        return false;
    }

    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
     stack<TreeNode*> pPath,qPath;
     getPath(root,p->val,pPath);
     getPath(root,q->val,qPath);

     int pSize=pPath.size();
     int qSize=qPath.size();

    int size;
     if(pSize>qSize){
         size=pSize-qSize;
         while(size--){
             pPath.pop();
         }
     }   else{
         size=qSize-pSize;
         while(size--){
             qPath.pop();
         }
     }

    while(!pPath.empty()){
        if(pPath.top()==qPath.top()){
            return pPath.top();
        }
        pPath.pop();
        qPath.pop();
       }
       return nullptr;
    }
};

JZ36 二叉搜索树与双向链表

https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5?tpId=13&tqId=11179&ru=/exam/oj

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};*/
class Solution {
  public:
    void InOrderConvert(TreeNode* cur, TreeNode*& prev) {//这里的prev是引用,所以一直有变化
        if (cur == nullptr)
            return;

        InOrderConvert(cur->left, prev);
        cur->left = prev;
        if (prev)
            prev->right = cur;
        prev = cur;
        InOrderConvert(cur->right, prev);
    }

    TreeNode* Convert(TreeNode* pRootOfTree) {
        TreeNode* prev = nullptr;
        InOrderConvert(pRootOfTree, prev);

        TreeNode* head = pRootOfTree;
        while (head && head->left) {
            head = head->left;
        }
        return head;
    }
};

105. 从前序与中序遍历序列构造二叉树

https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
 * right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* _buildTree(vector<int>& preorder, vector<int>& inorder, int& prei,int ibegin, int iend) {
        if (ibegin > iend)
            return nullptr;
        TreeNode* root = new TreeNode(preorder[prei]);
        int rooti =ibegin;
        while(rooti <= iend){
            if(inorder[rooti]==preorder[prei])
                break;
            else
            ++rooti;
        }
        ++prei;
        root->left=_buildTree(preorder,inorder,prei,ibegin,rooti-1);
        root->right=_buildTree(preorder,inorder,prei,rooti+1,iend);
        return root;    
    }

    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        int i=0;
        return _buildTree(preorder,inorder,i,0,inorder.size()-1);
    }
};

动态规划

JZ42 连续子数组的最大和

https://www.nowcoder.com/practice/459bd355da1549fa8a49e350bf3df484?tpId=13&tqId=11183&rp=1&ru=%2Factivity%2Foj&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking&tPage=2

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param array int整型vector 
     * @return int整型
     */
    int FindGreatestSumOfSubArray(vector<int>& array) {
        vector<int> v(array.size(),0);
        v[0]=array[0];
        int maxsum=v[0];
        for(int i=1;i<array.size();i++){
            v[i]=max(v[i-1]+array[i],array[i]);
            maxsum=max(maxsum,v[i]);
        }
        return maxsum;
    }
};

你可能感兴趣的:(各种算法题,算法)