栈
难度中等
题目描述
解题思路
遇到乘除立即算,遇到加减先入栈
https://leetcode-cn.com/problems/clumsy-factorial/solution/fu-xue-ming-zhu-yu-dao-cheng-chu-li-ji-s-furg/
Code
public int clumsy(int N) {Stack<Integer> stack = new Stack<>();int op = 0;int sum = 0;stack.push(N);for (int i = N - 1; i > 0; i--) {if (op == 0) {stack.push(stack.pop() * i);} else if (op == 1) {stack.push(stack.pop() / i);} else if (op == 2) {stack.push(i);} else {stack.push(-i);}op = (op + 1) % 4;}while (!stack.isEmpty()) {sum += stack.pop();}return sum;}
