coderwen
V2EX  ›  问与答

leetcode 有毒啊,是我眼睛花了么

  •  1
     
  •   coderwen · Aug 19, 2016 · 3026 views
    This topic created in 3583 days ago, the information mentioned may be changed or developed.

    今天做了两道 leetcode ,两道题都是第一组数据没过,关键自定义测试这组和本地测试这组数据都是正确的。 AC 比就这样被坑低了,以前也遇到过,今天一天遇两次,累觉不爱。 对了 我用的 golang ,是不是 golang 有毒

    第一道

    第二道

    Supplement 1  ·  Aug 19, 2016
    经 @virusdefender 提醒,我去掉了全局变量果然就 AC 了,也给大伙提个醒,不要图方便用全局变量
    但是还是很想知道为什么,我记得以前用 python 好像就没问题啊
    Supplement 2  ·  Aug 19, 2016

    以前看FAQ不仔细啊,我现在才看到 囧

    First, please check if you are using any global or static variables. They are Evil, period. If you must declare one, reset them in the first line of your called method or in the default constructor. Why? Because the judger executes all test cases using the same program instance, global/static variables affect the program state from one test case to another. See this Discuss thread for more details

    8 replies    2016-08-19 15:03:16 +08:00
    virusdefender
        1
    virusdefender  
       Aug 19, 2016
    你是不是用了全局变量之类的东西
    coderwen
        2
    coderwen  
    OP
       Aug 19, 2016
    @virusdefender
    你这么一说还真是,以前不用全局变量,今天图了方便,难道这其中有诈?
    virusdefender
        3
    virusdefender  
       Aug 19, 2016   ❤️ 1
    @coderwen 和 leetcode 评测流程有关,在自己的函数开头 reset 一下全局变量就好了。
    coderwen
        4
    coderwen  
    OP
       Aug 19, 2016
    @virusdefender 原来是这样,非常感谢
    skydiver
        5
    skydiver  
       Aug 19, 2016   ❤️ 1
    全局变量在不同 case 之间是共享的。
    coderwen
        6
    coderwen  
    OP
       Aug 19, 2016
    @skydiver 嗯,现在才意识到
    skydiver
        7
    skydiver  
       Aug 19, 2016
    @coderwen 所以也可以用来干坏事儿……
    longaiwp
        8
    longaiwp  
       Aug 19, 2016
    @skydiver 也可以干好事
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5132 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 56ms · UTC 09:18 · PVG 17:18 · LAX 02:18 · JFK 05:18
    ♥ Do have faith in what you're doing.