2019小米java开发

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Main {


/*请完成下面这个函数,实现题目要求的功能
当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^ 
******************************开始写代码******************************/
     static class Node{
        int val;
        Node left;
        Node right;

        Node(int val){
            this.val = val;
            left = null;
            right = null;
        }
    }
    static void solution(String input) {
        //构建二叉树
        Node root = buildTree(input);
        //中序遍历
        infixOrder(root);
    }

    //非递归实现中序遍历
    static void infixOrder(Node root){
        ArrayDeque<Node> stack = new ArrayDeque<>();
        Node current = root;
        while (current != null || !stack.isEmpty()){
            if(current != null){
                stack.push(current);
                current = current.left;
            }else{
                current = stack.pop();
                System.out.print(current.val);
                current = current.right;
            }
        }
    }



    //Example: 1(2(3,4(,5)),6(7,))
    static Node buildTree(String input){
        if(input == null || input.length() == 0){
            return null;
        }
        int i = Integer.valueOf(input.substring(0,1));
        Node root = new Node(i);
        if(input.length() > 1){
            String child = input.substring(2,input.length() - 1);
            int left = 0, right = 0, mid = 0;
            for (int j = 0; j < child.length(); j++){
                if(child.charAt(j) == '('){
                    left++;
                    continue;
                }
                if(child.charAt(j) == ')'){
                    right++;
                    continue;
                }
                if(child.charAt(j) == ',' && left == right){
                    mid = j;
                    break;
                }
            }
            String leftChild = child.substring(0,mid);
            String rightChild = child.substring(mid+1);
            root.left  = buildTree(leftChild);
            root.right = buildTree(rightChild);
        }
        return root;
    }

/******************************结束写代码******************************/


    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String res;
            
        String _input;
        try {
            _input = in.nextLine();
        } catch (Exception e) {
            _input = null;
        }
  
         solution(_input);
    }
}

第二题:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Main {

/*请完成下面这个函数,实现题目要求的功能
当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^ 
******************************开始写代码******************************/
    static int solution(int[] prices, int budget) {
 
        Arrays.sort(prices);
       
        int n = 0;
        for(int i = 0;i<prices.length ;i++){
            if(prices[i]>budget){
                break;
            }
            n++;
        }
 
 
        int[] dp = new int[budget+1];
 
        for (int i = 0; i <= budget; i++) {
            dp[i] = Integer.MAX_VALUE-1;
        }
 
        for (int i = 0; i < n; i++) {
            dp[prices[i]] = 1;
        }
 
        for (int i = 1; i <= budget; i++) {
            if(dp[i] == Integer.MAX_VALUE-1){
 
                for(int j=0;j<n;j++){
                    if(i-prices[j]>0){
                        dp[i] = Math.min(dp[i],dp[i-prices[j]]+1);
                    }
                }
            }
 
//            System.out.println(i+":"+Arrays.toString(dp));
        }
        return dp[budget]==Integer.MAX_VALUE-1?-1:dp[budget];

    }
/******************************结束写代码******************************/


    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int res;
            
        int _prices_size = 0;
        _prices_size = Integer.parseInt(in.nextLine().trim());
        int[] _prices = new int[_prices_size];
        int _prices_item;
        for(int _prices_i = 0; _prices_i < _prices_size; _prices_i++) {
            _prices_item = Integer.parseInt(in.nextLine().trim());
            _prices[_prices_i] = _prices_item;
        }
        
        int _budget;
        _budget = Integer.parseInt(in.nextLine().trim());
  
        res = solution(_prices, _budget);
        System.out.println(String.valueOf(res));    

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值