剑指 Offer 刷题记录

艾恩凝

个人博客  https://aeneag.xyz/ 

公众号 技术乱舞

本文仅记录个人刷题

2021/10/21

剑指offer 44

11/15

今天的这道题真是难受

class Solution {
public:
    int findNthDigit(int n) {
        int digit = 1;
        long start = 1;
        long count = 9;
        while (n > count) { 
            n -= count;
            digit += 1;
            start *= 10;
            count = digit * start * 9;
        }
        long num = start + (n - 1) / digit; 
        return to_string(num)[(n - 1) % digit]-'0';
    }
};

剑指offer 33

11/9

=====

class Solution {
public:
    bool dfs(vector<int>& postorder,int left ,int right){
        if(left > right)return true;
        int m = left, p = 0;
        while(postorder[m] < postorder[right])++m;
        p = m;
        while(postorder[m] > postorder[right])++m;
        return (right == m) && dfs(postorder,left,m-1) && dfs(postorder,m,right-1);
    }
    bool verifyPostorder(vector<int>& postorder) {
        return dfs(postorder,0,postorder.size()-1);
    }
};

剑指offer 60

11/8

class Solution {
public:
    vector<double> dicesProbability(int n) {
        vector<double> pre(6);
        for(int i=0;i<6;++i) pre[i]=1.0/6;
        for(int i = 2 ; i <= n ; ++i){
            vector<double> tmp(5*i+1,0);
            for(int k = 0 ; k < pre.size() ;k++){
                for(int j = 0 ; j < 6 ;j++){
                    tmp[k+j] += pre[k]/6;
                }
            }
            pre = tmp;
        }
        return pre;
    }
};

剑指offer 34

11/7

class Solution {
public:
    vector<vector<int>> res;
    vector<int> res_tmp;
    int target_tmp = 0;
    vector<vector<int>> pathSum(TreeNode* root, int target) {
        dfs(root,target);
        return res;
    }
    void dfs(TreeNode* root,int target){
        if(!root)return;
        target_tmp += root->val;
        res_tmp.push_back(root->val);
        if((target == target_tmp) && !(root->left) && !(root->right))res.push_back(res_tmp);
        dfs(root->left,target);
        dfs(root->right,target);
        target_tmp -= root->val;
        res_tmp.pop_back();
    }
};

剑指offer 32 加强版

11/5

有点东西

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        if(!root)return {};
        vector<vector<int>> res;
        deque<TreeNode*> dq;
        int layer = 1;
        dq.push_back(root);
        while(!dq.empty()){
            vector<int> tmp;
            int len = dq.size();
            while(len--){
                if(layer & 1){
                    tmp.push_back(dq.front()->val);
                    if(dq.front()->left)dq.push_back(dq.front()->left);
                    if(dq.front()->right)dq.push_back(dq.front()->right); 
                    dq.pop_front();
                }else{
                    tmp.push_back(dq.back()->val);
                    if(dq.back()->right)dq.push_front(dq.back()->right);
                    if(dq.back()->left)dq.push_front(dq.back()->left);
                    dq.pop_back();
                }
            }
            res.push_back(tmp);
            ++layer;
        }
        return res;
    }
};

剑指offer 32

11/4

层序遍历? 侮辱我智商, = =

class Solution {
public:
    vector<int> levelOrder(TreeNode* root) {
        if(!root)return {};
        queue<TreeNode*> q;
        vector<int> res;
        q.push(root);
        while(!q.empty()){
            if(q.front()->left)q.push(q.front()->left);
            if(q.front()->right)q.push(q.front()->right);
            res.push_back(q.front()->val);
            q.pop();
        }
        return res;
    }
};

剑指offer 49

11/4

class Solution {
public:
    int nthUglyNumber(int n) {
        int a = 0,b = 0,c = 0;
        vector<int> dp(n);
        dp[0] = 1;
        for(int i = 1 ; i < n ; ++i){
            int tmp2 = dp[a]*2,tmp3 = dp[b]*3,tmp5 = dp[c]*5;
            dp[i] = min(tmp2,min(tmp3,tmp5));
            if(dp[i] == tmp2)++a;
            if(dp[i] == tmp3)++b;
            if(dp[i] == tmp5)++c;
        }
        return dp[n-1];
    }
};

剑指offer 36

11/3

class Solution {
public:
    Node *head = nullptr, *tmp = nullptr;
    Node* treeToDoublyList(Node* root) {
        if(!root)return nullptr;
        dfs(root);
        head->left = tmp;
        tmp->right = head;
        return head;
    }
    void dfs(Node* node){
        if(!node)return ;
        dfs(node->left);
        if(!head)head = node;
        else tmp->right = node;
        node->left = tmp;
        tmp = node;
        dfs(node->right);
    }
};

剑指offer 56

11/2

麻木

class Solution {
public:
    vector<int> singleNumbers(vector<int>& nums) {
        int tmp = 0, m = 1,x = 0, y = 0;
        for(auto& c : nums)tmp ^= c;
        while((tmp & m) == 0) m <<= 1;
        for(auto& c : nums){
            if((c & m))x ^= c;
            else y ^= c;
        }
        return {x,y};
    }
};

剑指offer 55

21/10/27

这个系列的题确实简单多了

class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if(!root)return true;
        // int left = deep(root->left);
        // int right = deep(root->right);
        if(abs(deep(root->left) - deep(root->right)) > 1)return false;
        return isBalanced(root->left)&&isBalanced(root->right);
    }
    int deep(TreeNode* root){
        if(!root)return 0 ;
        return max(deep(root->left),deep(root->right))+1;
    }
};

剑指offer 32

21/10/25

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode*> q;
        vector<vector<int>> res;
        if(!root)return res;
        q.push(root);
        while(!q.empty()){
            vector<int> tmp;
            int s = q.size();
            for(int i = 0 ; i < s ; ++i){
                tmp.push_back(q.front()->val);
                if(q.front()->left)q.push(q.front()->left);
                if(q.front()->right)q.push(q.front()->right);
                q.pop();
            }
            res.push_back(tmp);
        }
        return res;
    }
};

剑指offer 57

21/10/24

class Solution {
public:
    vector<vector<int>> findContinuousSequence(int target) {
        vector<vector<int>> res;
        int left = 1, right = 2, sum = 3;
        while(left < right){
            if(sum == target){
                vector<int> r;
                for(int i = left ; i <= right ; ++i) r.push_back(i);
                res.push_back(r);
            }
            if(sum < target){
                ++right;
                sum += right;
            }else if (sum >= target){
                sum -= left;
                ++left;
            }
        }
        return res;
    }
};

剑指offer 68

21/10/23

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == nullptr || root == p || root == q) return root;
        TreeNode *left = lowestCommonAncestor(root->left, p, q);
        TreeNode *right = lowestCommonAncestor(root->right, p, q);
        if(left == nullptr && right == nullptr) return nullptr; 
        if(left == nullptr) return right; 
        if(right == nullptr) return left; 
        return root; 
    }
};

剑指offer 17 打印从1到最大的n位数

2021/10/23

class Solution {
public:
    vector<int> res;
    vector<int> printNumbers(int n) {
        string path(n,'0');
        dfs(n,0,path);
        return res;
    }
    void dfs(int n,int x,string str){
        if(x == n){
            int val = std::stoi(str);
            if(val)res.push_back(val);
            return;
        }
        for(int i = 0 ; i < 10 ; ++i ){
            str[x] = i + '0';
            dfs(n,x+1,str);
        }
    }
};

剑指offer 29

21/10/22

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        if(!matrix.size()) return {};
        vector<int> res;
        int left = 0,right = matrix[0].size()-1,top = 0,bottom = matrix.size()-1;
        while(true){
            for(int i = left ; i <= right;++i)res.push_back(matrix[top][i]);
            if(++top > bottom)break;
            for(int i = top ; i <= bottom;++i)res.push_back(matrix[i][right]);
            if(--right < left)break;
            for(int i = right ; i >= left;--i)res.push_back(matrix[bottom][i]);
            if(--bottom < top)break;
            for(int i = bottom ; i >= top;--i)res.push_back(matrix[i][left]);
            if(++left > right)break;
        }
        return res;
    }
};

剑指offer 54

21/10/22

显然可以做

class Solution {
public:
    int count = 0,res = 0;
    int kthLargest(TreeNode* root, int k) {
        dfs(root,k);
        return res;
    }
    void dfs(TreeNode* node,int k){
        if(!node)return;
        dfs(node->right,k);
        ++count;
        if(count == k) res = node->val;
        dfs(node->left,k);
    }
};

剑指 Offer 27

照镜子呗,其实就是二叉树左右节点呼唤位置

class Solution {
public:
    TreeNode* mirrorTree(TreeNode* root) {
        if(!root)return nullptr;
        TreeNode* tmp = root->right;
        root->right = mirrorTree(root->left);
        root->left = mirrorTree(tmp);
        return root;
    }
};

剑指 Offer 21

双指针

class Solution {
public:
    vector<int> exchange(vector<int>& nums) {
        int i = 0 , j = nums.size()-1,p=0;
        while(i < j){
            while(i<j && nums[i]%2 == 1)++i;
            while(i<j && nums[j]%2 == 0)--j;
            swap(nums[i++],nums[j--]);
        }
        return nums;
    }
};

题目太简单

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾恩凝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值