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

 

posted on 2019-05-14 19:09  小明明明不是企鹅  阅读(236)  评论(0编辑  收藏  举报