LeetCodeDiary

A Diary for solving LeetCode problems

View on GitHub
class Solution(object):
    def clumsy(self, N):
        op = 0
        stack = [N]
        for i in range(N - 1, 0, -1):
            if op == 0:
                stack.append(stack.pop() * i)
            elif op == 1:
                stack.append(int(stack.pop() / float(i)))
            elif op == 2:
                stack.append(i)
            elif op == 3:
                stack.append(-i)
            op = (op + 1) % 4
        return sum(stack)

数学 中等 👆一般栈写法

👇找规律写法

class Solution:
    def clumsy(self, N: int) -> int: 
        if N==1:
            return 1
        if N==2:
            return 2
        if N==3:
            return 6
        if N==4:
            return 7
        if N>4:
            if N%4==0:
                return N+1
            if N%4==3:
                return N-1
            else:
                return N+2