754. Reach a Number

You are standing at position 0 on an infinite number line. There is a goal at position target.

On each move, you can either go left or right. During the n-th move (starting from 1), you take n steps.

Return the minimum number of steps required to reach the destination.

Example 1:

Input: target = 3
Output: 2
Explanation:
On the first move we step from 0 to 1.
On the second step we step from 1 to 3.

Example 2:

Input: target = 2
Output: 3
Explanation:
On the first move we step from 0 to 1.
On the second move we step  from 1 to -1.
On the third move we step from -1 to 2.

Note:

  • target will be a non-zero integer in the range [-10^9, 10^9].
class Solution:
    def reachNumber(self, target):
        """
        :type target: int
        :rtype: int
        """
        t = abs(target)
        n,sum = 0,0
        while t>sum:
            n += 1
            sum += n
        if t==sum:
            return n
        remain = sum-target
        if remain%2==0:
            return n
        else:
            return n+1 if(n%2)==0 else n+2

完全是找数字规律的题目,如果用dfs来搜索的话内存会超。

参考: https://blog.csdn.net/u014688145/article/details/78948918

原文地址:https://www.cnblogs.com/bernieloveslife/p/10263251.html

时间: 2024-08-30 12:35:00

754. Reach a Number的相关文章

[LeetCode] Reach a Number 达到一个数字

You are standing at position 0 on an infinite number line. There is a goal at position target. On each move, you can either go left or right. During the n-th move (starting from 1), you take n steps. Return the minimum number of steps required to rea

到达一个数 Reach a Number

2018-09-24 14:19:58 问题描述: 问题求解: 初看到这个问题,直觉上认为可以通过BFS遍历解空间进行求解,因为本质上来说,这个问题和棋盘上移动马的问题是一类问题,都是可以转化成图的问题,但是MLE了,问题出在在本问题中是不能使用used来保存已经扩展过的节点的,因为相同的节点在不同的阶段的移动步数是不一样的,因此都需要进行入队列的操作. 当然,看到数据规模就应该有意识,这个问题是不能使用暴力搜索来求解的. 事实上,这个问题是一个数学问题,求解方案是: 1)首先负数和其相反数的步

Draft-TCAD

IEEEtran.cls 1 %% 2 %% IEEEtran.cls 2007/03/05 version V1.7a 3 %% 4 %% 5 %% This is the official IEEE LaTeX class for authors of the Institute of 6 %% Electrical and Electronics Engineers (IEEE) Transactions journals and 7 %% conferences. 8 %% 9 %% S

Draft-JSS模板

IEEEtran.cls 1 %% 2 %% IEEEtran.cls 2007/03/05 version V1.7a 3 %% 4 %% 5 %% This is the official IEEE LaTeX class for authors of the Institute of 6 %% Electrical and Electronics Engineers (IEEE) Transactions journals and 7 %% conferences. 8 %% 9 %% S

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,

LeetCode(386)Lexicographical Numbers

题目 Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000. 分析 给定一个整数,将1~n内的n个整数

HBase写入性能改造(续)--MemStore、flush、compact参数调优及压缩卡的使用【转】

首先续上篇测试: 经过上一篇文章中对代码及参数的修改,Hbase的写入性能在不开Hlog的情况下从3~4万提高到了11万左右. 本篇主要介绍参数调整的方法,在HDFS上加上压缩卡,最后能达到的写入性能为17W行每秒(全部测试都不开Hlog). 上篇测试内容: 详情 http://blog.csdn.net/kalaamong/article/details/7275242. 测试数据 http://blog.csdn.net/kalaamong/article/details/7290192 同

大开测试:性能- 基于目标和手动场景测试有何联系和不同(连载20)

7.20  基于目标和手动场景测试有何联系和不同 1.问题提出 在应用LoadRunner的Controller进行性能测试场景的设计时,有两种方案可以对场景进行设置,一种为手工方式,另一种为基于目标方式,那么在什么情况下针对性地选择这两种方式,它们之间有什么联系和不同呢? 2.问题解答 要使用LoadRunner进行系统性能测试,对系统进行负载,必须创建一个场景.场景中包含关于测试会话信息的文件.场景是一种模拟实际用户的方式.场景包含有关如何模拟实际用户的信息:虚拟 用户组.测试脚本以及用于运

weblogic 12 套接字泄露处理方法

现场weblogic报<DynamicListenThread[Default]> <<WLS Kernel>> <> <> <1417715701920> <BEA-002631> <Max threshold reached for open sockets [total open sockets: 5,000, allowed max: 5,000]. No more connections allowed.&