题解 | #累加出整个范围所有的数最少还需要几个数#

公式字符串求值

http://www.nowcoder.com/practice/c590e97ee1f6462d871430feee055d25

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

public class Main{

public static void main(String[] args) throws IOException {
    BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
    String input = sc.readLine();

    int result = process(input);
    System.out.println(result);
}

public static int process(String input) {

    char[] ch = input.toCharArray();

    Stack<Character> operatorStack = new Stack<>();
    Stack<Integer> dataStack = new Stack<>();
    if (ch[0] == '-') {
        dataStack.push(0);
    }


    int count = 0;
    for (int i = 0; i < ch.length; i++) {
        if (ch[i] >= '0' && ch[i] <= '9') {
            count++;
            if (i + 1 < ch.length && !(ch[i + 1] >= '0' && ch[i + 1] <= '9')) {
                String num = input.substring(i - count + 1, i + 1);
                dataStack.push(Integer.parseInt(num));
                count = 0;
            } else if (i == ch.length - 1) {
                String num = input.substring(i - count + 1);
                dataStack.push(Integer.parseInt(num));
            }
        } else if (ch[i] == '(') {
            operatorStack.push(ch[i]);
        } else if (ch[i] == '*' || ch[i] == '/') {
            while (operatorStack.size() != 0 && (operatorStack.peek() == '*' || operatorStack.peek() == '/')) {
                Integer passiveNum = dataStack.pop();
                Integer ObjectiveNum = dataStack.pop();
                Character operator = operatorStack.pop();
                Integer value = -1;
                if (operator == '*') {
                    value = ObjectiveNum * passiveNum;
                } else if (operator == '/') {
                    value = ObjectiveNum / passiveNum;
                }
                dataStack.push(value);
            }
            operatorStack.push(ch[i]);
        } else if (ch[i] == '+' || ch[i] == '-') {

            if (i-1>=0&&ch[i]=='-'&&ch[i-1]=='('){
                dataStack.push(0);
            }

            if (operatorStack.size() == 0 || (operatorStack.size() > 1 && operatorStack.peek() != '*' && operatorStack.peek() != '/' && operatorStack.peek() != '+' && operatorStack.peek() != '-')) {
                operatorStack.push(ch[i]);
            } else {
                while (operatorStack.size() != 0 && (operatorStack.peek() == '*' || operatorStack.peek() == '/' || operatorStack.peek() == '+' || operatorStack.peek() == '-')) {
                    Integer passiveNum = dataStack.pop();
                    Integer ObjectiveNum = dataStack.pop();
                    Character operator = operatorStack.pop();
                    Integer value = -1;
                    if (operator == '*') {
                        value = ObjectiveNum * passiveNum;
                    } else if (operator == '/') {
                        value = ObjectiveNum / passiveNum;
                    } else if (operator == '+') {
                        value = ObjectiveNum + passiveNum;
                    } else if (operator == '-') {
                        value = ObjectiveNum - passiveNum;
                    }
                    dataStack.push(value);
                }
                operatorStack.push(ch[i]);
            }
        } else if (ch[i] == ')') {
            while (operatorStack.size() != 0 && operatorStack.peek() != '(') {
                Integer passiveNum = dataStack.pop();
                Integer ObjectiveNum = dataStack.pop();
                Character operator = operatorStack.pop();
                Integer value = -1;
                if (operator == '*') {
                    value = ObjectiveNum * passiveNum;
                } else if (operator == '/') {
                    value = ObjectiveNum / passiveNum;
                } else if (operator == '+') {
                    value = ObjectiveNum + passiveNum;
                } else if (operator == '-') {
                    value = ObjectiveNum - passiveNum;
                }
                dataStack.push(value);
            }
            operatorStack.pop();
        }
    }


    while (!operatorStack.isEmpty()) {
        Integer passiveNum = dataStack.pop();
        Integer ObjectiveNum = dataStack.pop();
        Character operator = operatorStack.pop();
        Integer value = -1;
        if (operator == '*') {
            value = ObjectiveNum * passiveNum;
        } else if (operator == '/') {
            value = ObjectiveNum / passiveNum;
        } else if (operator == '+') {
            value = ObjectiveNum + passiveNum;
        } else if (operator == '-') {
            value = ObjectiveNum - passiveNum;
        }
        dataStack.push(value);
    }
    return dataStack.peek();
}

}

全部评论

相关推荐

10-09 09:39
门头沟学院 C++
HHHHaos:这也太虚了,工资就一半是真的
点赞 评论 收藏
分享
粗心的雪碧不放弃:纯学历问题,我这几个月也是一直优化自己的简历,后来发现优化到我自己都觉得牛逼的时候,发现面试数量也没有提升,真就纯学历问题
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务