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));
}
}