leetcode36——所有因子,先序遍历二叉搜索树,粉刷房子

本文介绍了LeetCode中的三道题目,涉及寻找整数的因子组合、判断给定数组是否为二叉搜索树的先序遍历序列以及求解粉刷房子的最小成本。通过对这些问题的解析,帮助读者加深对算法和数据结构的理解。
摘要由CSDN通过智能技术生成

leetcode254:
整数可以被看作是其因子的乘积。
例如:
8 = 2 x 2 x 2;
= 2 x 4.
请实现一个函数,该函数接收一个整数 n 并返回该整数所有的因子组合。
注意:
你可以假定 n 为永远为正数。
因子必须大于 1 并且小于 n。
示例 1:
输入: 1
输出: []
示例 2:
输入: 37
输出: []
示例 3:
输入: 12
输出:
[
[2, 6],
[2, 2, 3],
[3, 4]
]

class Solution {
    List<List<Integer>> result=new LinkedList<>();
    public List<List<Integer>> getFactors(int n) {
        if(n==1)return result;
        getValue(2,n,n,new LinkedList<>());
        return result;
    }
    public void getValue(int start,int value ,int n,LinkedList<Integer> list){
        if(value==1)
        {result.add(new LinkedList(list));return; }
        for(int i=start;i<n;i++){
            if(value%i==0){
                list.add(i);
                getValue(i,value/i,n,list);
                list.remove(list.size()-1);
            }
        }
    }
}

leetcode255:
给定一个整数数组,你需要验证它是否是一个二叉搜索树正确的先序遍历序列。
你可以假定该序列中的数都是不相同的。
参考以下这颗二叉搜索树:

 5
/ \

2 6
/
1 3
示例 1:
输入: [5,2,6,1,3]
输出: false
示例 2:
输入: [5,2,1,3,6]
输出: true

class Solution {
    public boolean verifyPreorder(int[] preorder) {
        if(preorder.length==0)return true;
        return judgeTree(0,preorder.length-1,preorder,preorder[0]);
    }
    public boolean judgeTree(int start,int end,int []num,int rootValue){
        if(start>end)return true;
        if(start==end)return true;
        int left=start+1;
        while(left<=end&&num[left]<num[start]){
            left++;
        }
        int va=left;
        while(va<=end){
            if(num[start]>num[va])return false;
            va++;
        }
        boolean IsLeft=judgeTree(start+1,left-1,num,num[start]);
        boolean IsRight=judgeTree(left,end,num,num[start]);
        return IsLeft&&IsRight;
    }
}

leetcode:256
假如有一排房子,共 n 个,每个房子可以被粉刷成红色、蓝色或者绿色这三种颜色中的一种,你需要粉刷所有的房子并且使其相邻的两个房子颜色不能相同。

当然,因为市场上不同颜色油漆的价格不同,所以房子粉刷成不同颜色的花费成本也是不同的。每个房子粉刷成不同颜色的花费是以一个 n x 3 的矩阵来表示的。

例如,costs[0][0] 表示第 0 号房子粉刷成红色的成本花费;costs[1][2] 表示第 1 号房子粉刷成绿色的花费,以此类推。请你计算出粉刷完所有房子最少的花费成本。
注意:
所有花费均为正整数。
示例:
输入: [[17,2,17],[16,16,5],[14,3,19]]
输出: 10
解释: 将 0 号房子粉刷成蓝色,1 号房子粉刷成绿色,2 号房子粉刷成蓝色。
最少花费: 2 + 5 + 3 = 10。

class Solution {
    public int minCost(int[][] costs) {
        if(costs.length==0)return 0;
        int dp[]=new int [costs[0].length];
        int tmp[]=new int [costs[0].length];
        dp[0]=costs[0][0];
        dp[1]=costs[0][1];
        dp[2]=costs[0][2];
        for(int i=0;i<costs.length;i++){
            dp[0]=Math.min(tmp[1],tmp[2])+costs[i][0];
            dp[1]=Math.min(tmp[0],tmp[2])+costs[i][1];
            dp[2]=Math.min(tmp[0],tmp[1])+costs[i][2];
            for(int j=0;j<costs[0].length;j++){
                tmp[j]=dp[j];
            }
        }
        return Math.min(Math.min(dp[0],dp[1]),dp[2]);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值