活跃农民
- 积分
- 371
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2013-3-31
- 最后登录
- 1970-1-1
|
brute force...
- public class Solution {
- public static void main(String[] args) {
- Solution s = new Solution();
- String str = "9+3*2+3";
- System.out.println(str);
- System.out.println(s.getExpMax(str));
- }
- public int getExpMax(String s) {
- int n = s.length();
- int ind = 0;
- int max = 0;
- List<Integer> finalLast = new ArrayList<>();
- List<Integer> finalRes = new ArrayList<>();
- List<Integer> openLast = new ArrayList<>();
- List<Integer> openRes = new ArrayList<>();
- List<Integer> openPrev = new ArrayList<>();
- List<Integer> openPrevLast = new ArrayList<>();
- List<Character> openOp = new ArrayList<>();
- //note the first element in the final list is the version without brackets
- finalLast.add(0);
- finalRes.add(0);
- int curVal = 0;
- char op = '+';
- while (ind < n) {
- if (s.charAt(ind) >= '0' && s.charAt(ind) <= '9') {
- while (ind < n && Character.isDigit(s.charAt(ind))) {
- curVal = curVal * 10 + s.charAt(ind) - '0';
- ind++;
- }
- //after reading a value we should do calculation
- //do final calculation first
- int prevRes = finalRes.get(0);
- int prevResLast = finalLast.get(0);
- calculateList(finalLast, finalRes, curVal, op);
- calculateList(openLast, openRes, curVal, op);
- //now we can close the brackets and add it to final
- for (int i = 0; i < openRes.size(); i++) {
- int prevResult = openPrev.get(i);
- char prevOp = openOp.get(i);
- int closeVal = openRes.get(i);
- int prevResultLast = openPrevLast.get(i);
- if (prevOp == '+') {
- finalRes.add(prevResult + closeVal);
- finalLast.add(closeVal);
- } else if (prevOp == '-') {
- finalRes.add(prevResult - closeVal);
- finalLast.add(-closeVal);
- } else if (prevOp == '*') {
- finalRes.add(prevResult - prevResultLast + prevResultLast * closeVal);
- finalLast.add(prevResultLast * closeVal);
- } else {
- finalRes.add(prevResult - prevResultLast + prevResultLast/closeVal);
- finalLast.add(prevResultLast/closeVal);
- }
- }
- //add to open, means this element is the starting of the bracket
- openLast.add(curVal);
- openRes.add(curVal);
- openOp.add(op);
- openPrev.add(prevRes);
- openPrevLast.add(prevResLast);
- curVal = 0;
- } else if (s.charAt(ind) == ' ') ind++;
- else {
- //operators
- op = s.charAt(ind);
- ind++;
- }
- }
- System.out.println("list of results");
- for (int r : finalRes) {
- System.out.println(r);
- }
- max = Collections.max(finalRes);
- return max;
- }
- public void calculateList(List<Integer> finalLast, List<Integer> finalRes, int curVal, char op) {
- for (int i = 0; i < finalLast.size(); i++) {
- int last = finalLast.get(i);
- int left = finalRes.get(i);
- if (op == '+') {
- finalRes.set(i, left+curVal);
- finalLast.set(i, curVal);
- } else if (op == '-') {
- finalRes.set(i, left-curVal);
- finalLast.set(i, -curVal);
- } else if (op == '*') {
- finalRes.set(i, left-last+last*curVal);
- finalLast.set(i, last*curVal);
- } else {
- finalRes.set(i, left-last+last/curVal);
- finalLast.set(i, last/curVal);
- }
- }
- }
- }
- /*
- 9+3*2+3
- list of results
- 18
- 27
- 18
- 18
- 18
- 18
- 24
- */
复制代码 |
|