leetcode 754 到达终点数字

https://blog.csdn.net/u012737193/article/details/78951070

class Solution(object):
    def reachNumber(self, target):
        """
        :type target: int
        :rtype: int
        """
        target = abs(target)
        i = 0
        ans = 0
        while ans < target:
            i += 1
            ans += i
        dt = ans - target
        if ans == target:
            return i
        if dt%2 == 0:
            return i
        else:
            if i % 2 == 0:
                return i+1
            else:
                return i+2
        
        return i+1

猜你喜欢

转载自www.cnblogs.com/woshizhizhang/p/10864004.html