Day9:刷题

34.在这里插入图片描述

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* left;
    Node* right;

    Node() {}

    Node(int _val) {
        val = _val;
        left = NULL;
        right = NULL;
    }

    Node(int _val, Node* _left, Node* _right) {
        val = _val;
        left = _left;
        right = _right;
    }
};
*/
class Solution {
public:
Node * head=NULL;
Node* pre=NULL;
Node * tail=NULL;
void inorder1(Node* root){
    if(!root){
        return;
    }
    inorder1(root->left);
    if(pre==NULL){//第一个元素
        head=root;
    }
    else{
        pre->right=root;//连接后继
    }
    root->left=pre;//连接前驱
    pre=root;//指向新的节点
    tail=root;
    inorder1(root->right);
    return;
}
Node* treeToDoublyList(Node* root){
    if(!root){
        return root;
    }
    inorder1(root);//中序遍历为元素递增排列
    head->left=tail;
    tail->right=head;
    return head;
}
};

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Codec {
public:

    // Encodes a tree to a single string.
  string serialize(TreeNode* root) {
        string res="";
        queue<TreeNode*> q;//基于队列层次遍历
        q.push(root);
        while (!q.empty()) {
            TreeNode* temp = q.front();
            q.pop();
            if (temp) {
                res+=to_string(temp->val);//变为字符串
                res+=" ";
                q.push(temp->left);
                q.push(temp->right);
            } else {
                res+="null ";
            }
        }
        return res;
    }

    // Decodes your encoded data to tree.通过序列建树(leetcode算法)
    TreeNode* deserialize(string data) {
        istringstream input(data);//它的作用是从string对象data中读取字符
        string val;
        vector<TreeNode*> vec;
        while (input >> val) {//读取的字符
            if (val == "null") {
                vec.push_back(NULL);
            } else {
                vec.push_back(new TreeNode(stoi(val)));//stoi,把字符串转换为十进制数
            }
        }
        int j = 1;                                          // i每往后移动一位,j移动两位,j始终是当前i的左子下标
        for (int i = 0; j < vec.size(); ++i) {              // 肯定是j先到达边界,所以这里判断j < vec.size()
            if (vec[i] == NULL) continue;                   // vec[i]为null时跳过。
            if (j < vec.size()) vec[i]->left = vec[j++];    // 当前j位置为i的左子树
            if (j < vec.size()) vec[i]->right = vec[j++];   // 当前j位置为i的右子树
        }
        return vec[0];
    }

};

// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));


36.全排列(dfs)
在这里插入图片描述

class Solution {
public:
    vector<string> result;
    vector<string> permutation(string s) {
        dfs(s, 0);
        return result;
    }
    void dfs(string& s, int pos) {
        if (pos >= s.size()) {
            result.push_back(s);
            return;
        }
        for (int i = pos; i < s.size(); ++i) {
            if (judge(s, pos, i)) continue;   // 如果pos和i之间有字符等于s[i],则跳过。
            swap(s[pos], s[i]);
            dfs(s, pos+1);
            swap(s[pos], s[i]);
        }
    }

    bool judge(string& s, int start, int end) {
        for (int i = start; i < end; ++i) {
            if (s[i] == s[end]) return true;
        }
        return false;
    }
};

在这里插入图片描述

/**
 * 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:
vector<vector<int>> res;
vector<int> arr;
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        
        dfs(root,sum);
        return res;

    }
    void dfs(TreeNode* root, int sum){
        if(root==nullptr){
            return ;
        }
        arr.push_back(root->val);//根
        sum-=root->val;//sum 在调用的子函数中是改变的
        if(root->left==nullptr&&root->right==nullptr&&sum==0){//到达叶子节点
            res.push_back(arr);
        }
        dfs(root->left,sum);
        dfs(root->right,sum);
        arr.pop_back();//将之前记录的路径清空 方便下一次记录
    }

};

在这里插入图片描述

class Solution {//从个位遍历到最高位
public:
    int countDigitOne(int n) {
       int count = 0;
       long i = 1;//指向遍历的位数,如i=1即个位,i=10即十位,...,因为n可以有31位,所以10^31用long存储
       while(n/i!=0){
           //n/i控制遍历的次数,将所有的位数都遍历完毕
            long high = n/(10*i);//将当前位之前的所有高位都存在high中
            long cur = (n/i)%10;//将当前位记录在cur中,即我们每次都需要统计当前位上1出现的次数
            long low = n-(n/i)*i;
            if(cur == 0){//假设高位为high,当前位为cur,低位为low,i代表着需要统计的位置数(1/                         //对应个位,10对应十位,100对应百位),则对每一位的个数count有:
                         //cur=0,count = high*i;
                         //cur=1,count=high*i+low+1;
                         //cur>1,count=high*i+i
                count += high * i;
            } else if(cur == 1){
                count += high * i + low + 1;
            } else {
                count += high * i + i;
            }
            i = i * 10;//准备遍历下一位
       }
       return count;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值