Siam_ese 2021-06-17 20:03 采纳率: 50%
浏览 28
已采纳

求问以下的前缀表达式代码应该怎么优化

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class PrefixExpression5 {
	
	// store some constant values
	final static long num = 1000000007;
	final static long max = Long.MAX_VALUE;
	
	public static void main(String[] args) throws IOException {
		
		// construct a stack to store the calculate values
		Stack<Long> st = new Stack<>();
		// get the array that stores the input data
		String[] elements = readData();
		// calculate the result
        long result = calculate(elements, st);
        // the prefix expression is invalid
        if (result == max) {
        	System.out.println("Invalid");
        }
        // the prefix expression is valid
        else {
        	// handle the result
        	result = (result + num) % num;
        	System.out.println(result);
        }
    }
    
	public static String[] readData() throws IOException {
		// construct a BufferedReader instance
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        // real the first line
        // split the first line
        // read n and k respectively
        String line = br.readLine();
        int n = Integer.parseInt(line);
        //System.out.println(n);
		String[] elements = new String[n];
		// read the data
        for (int i = 0; i < n; i++) {
        	elements[i] = br.readLine();
        }
        br.close();
        return elements;
	}
	
	public static long calculate(String[] arr, Stack<Long> st) {
		
		// store some values that will be used later
		String plus = "+";
		String minus = "-";
		long calRes;
		
		for (int i = arr.length - 1; i >= 0; i--) {
			String temp = arr[i];
			// if temp is numeric
			if (temp.toString().matches("^[0-9]*$")) {
				// push the numeric value to the stack
				st.push(Long.parseLong(arr[i]) % num);
			}
			else {
				// the expression is not correct
				if (st.size() < 2) {
					return max;
				}
				// pop two numbers from the stack and calculate
				long a = st.pop();
				long b = st.pop();
				if (temp.equals(plus)) {
					calRes = (a + b) % num;
				}
				else if (temp.equals(minus)) {
					calRes = ((a - b) % num);
				}
				else {
					calRes = ((a * b) % num);
				}
				// push the result into the stack
				st.push(calRes);
				}
			}
		// return the result
		return st.pop();
	}
}

输入样例

5
*
10
+
20
30

输出

500

 

oj测试最后一个点超过1s限制没过

 

求问应如何优化

  • 写回答

2条回答 默认 最新

  • 会飞的基德 2021-06-18 17:56
    关注

    使用双端队列Deque代替Stack,应该会加快你代码的效率,使用数组效率可能会更高,但要看你怎么写了,代码可能会变的很恶心,建议Deque代替Stack

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已采纳回答 7月16日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度