【CODE】丑数 & 判断二叉搜索树

丑数

把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。

class Solution {
public:
    int GetUglyNumber_Solution(int index) {
        if (index <= 6) return index;
        vector<int> res(index);
        res[0]=1;
        int t2=0,t3=0,t5=0;
        for(int i=1;i<index;i++){
            res[i]=min(res[t2]*2,min(res[t3]*3,res[t5]*5));
            if(res[i]==res[t2]*2) t2++;
            if(res[i]==res[t3]*3) t3++;
            if(res[i]==res[t5]*5) t5++;
        }
        return res[index-1];
    }
};

313. 超级丑数

难度中等90

编写一段程序来查找第 n 个超级丑数。

超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数。

示例:

输入: n = 12, primes= [2,7,13,19]
输出: 32 
解释: 给定长度为 4 的质数列表 primes = [2,7,13,19],前 12 个超级丑数序列为:[1,2,4,7,8,13,14,16,19,26,28,32] 。

说明:

  • 1 是任何给定 primes 的超级丑数。
  •  给定 primes 中的数字以升序排列。
  • 0 < k ≤ 100, 0 < n ≤ 106, 0 < primes[i] < 1000 。
  • 第 n 个超级丑数确保在 32 位有符整数范围内。
class Solution {
public:
    int nthSuperUglyNumber(int n, vector<int>& primes) {
        vector<int> ti(primes.size());
        vector<int> res(n);
        res[0]=1;
        for(int i=1;i<n;i++){
            int tmp=INT_MAX;
            vector<int> flag;
            for(int j=0;j<primes.size();j++){
                if(tmp>(primes[j]*res[ti[j]])){
                    flag.clear();
                    flag.push_back(j);
                    tmp=primes[j]*res[ti[j]];
                }else if(tmp==(primes[j]*res[ti[j]])){
                    flag.push_back(j);
                }
            }
            for(int j=0;j<flag.size();j++) ti[flag[j]]++;
            res[i]=tmp;
            
        }
        return res[n-1];
    }
};

263. 丑数

难度简单131

编写一个程序判断给定的数是否为丑数。

丑数就是只包含质因数 2, 3, 5 的正整数

示例 1:

输入: 6
输出: true
解释: 6 = 2 × 3

示例 2:

输入: 8
输出: true
解释: 8 = 2 × 2 × 2

示例 3:

输入: 14
输出: false 
解释: 14 不是丑数,因为它包含了另外一个质因数 7。
class Solution {
public:
    bool isUgly(int num) {
        if(num<=0) return false;
        if(num<=6) return true;
        vector<int> tmp={2,3,5};
        for(auto tmpp:tmp){
            while(num%tmpp==0) num/=tmpp;
        }
        if(num==1) return true;
        return false;
    }
};

说明:

  1. 1 是丑数。
  2. 输入不会超过 32 位有符号整数的范围: [−231,  231 − 1]。

98. 验证二叉搜索树

难度中等523收藏分享切换为英文关注反馈

给定一个二叉树,判断其是否是一个有效的二叉搜索树。

假设一个二叉搜索树具有如下特征:

  • 节点的左子树只包含小于当前节点的数。
  • 节点的右子树只包含大于当前节点的数。
  • 所有左子树和右子树自身必须也是二叉搜索树。

示例 1:

输入:
    2
   / \
  1   3
输出: true

示例 2:

输入:
    5
   / \
  1   4
     / \
    3   6
输出: false
解释: 输入为: [5,1,4,null,null,3,6]。
     根节点的值为 5 ,但是其右子节点值为 4 。
  • 中序遍历
  • 递归
/**
 * 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 isValidBST(TreeNode* root) {
        if(root==NULL) return true;
        stack<TreeNode*> st;
        int tag=0,last=0;
        while(!st.empty()||root){
            while(root){
                st.push(root);
                root=root->left;
            }
            TreeNode *tmp=st.top();
            st.pop();
            if(tag==0){
                last=tmp->val;
                tag=1;
            }else{
                if(tmp->val<=last) return false;
                else last=tmp->val;
            }
            if(tmp->right) root=tmp->right;
        }
        return true;
    }
};
class Solution {
public:
    bool helper(TreeNode *root,long long int lower,long long int upper ){
        //考虑以root为根的子树,节点值是否在lower和upper之间
        if(root==NULL) return true;
        if(root->val<=lower || root->val>=upper) return false;
        return helper(root->left,lower,root->val) && helper(root->right,root->val,upper);
    }
    bool isValidBST(TreeNode* root) {
        return helper(root,LONG_MIN,LONG_MAX);
    }
};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值