Leetcode.1006.笨阶乘
题目难度:中等
题目:原题链接
思路
class Solution {
public int clumsy(int N) {
Deque<Integer> stack = new LinkedList<Integer>();
stack.push(N);
N--;
//控制运算符号 * / + -
int index = 0;
while(N > 0)
{
if(index % 4 == 0)
{
stack.push(stack.pop()*N);
}
else if(index % 4 == 1)
{
stack.push(stack.pop()/N);
}
else if(index % 4 == 2)
{
stack.push(stack.pop()+N);
}
else
{
stack.push(-N);
}
index++;
N--;
}
int sum = 0;
while(!stack.isEmpty())
{
sum += stack.pop();
}
return sum;
}
}