V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
coderwen
V2EX  ›  问与答

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

  •  1
     
  •   coderwen · 2016-08-19 13:23:41 +08:00 · 2516 次点击
    这是一个创建于 2798 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    第一道

    第二道

    第 1 条附言  ·  2016-08-19 13:56:48 +08:00
    经 @virusdefender 提醒,我去掉了全局变量果然就 AC 了,也给大伙提个醒,不要图方便用全局变量
    但是还是很想知道为什么,我记得以前用 python 好像就没问题啊
    第 2 条附言  ·  2016-08-19 14:01:09 +08:00

    以前看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 条回复    2016-08-19 15:03:16 +08:00
    virusdefender
        1
    virusdefender  
       2016-08-19 13:25:06 +08:00
    你是不是用了全局变量之类的东西
    coderwen
        2
    coderwen  
    OP
       2016-08-19 13:26:28 +08:00
    @virusdefender
    你这么一说还真是,以前不用全局变量,今天图了方便,难道这其中有诈?
    virusdefender
        3
    virusdefender  
       2016-08-19 13:56:46 +08:00   ❤️ 1
    @coderwen 和 leetcode 评测流程有关,在自己的函数开头 reset 一下全局变量就好了。
    coderwen
        4
    coderwen  
    OP
       2016-08-19 13:57:33 +08:00
    @virusdefender 原来是这样,非常感谢
    skydiver
        5
    skydiver  
       2016-08-19 13:59:35 +08:00   ❤️ 1
    全局变量在不同 case 之间是共享的。
    coderwen
        6
    coderwen  
    OP
       2016-08-19 14:01:56 +08:00
    @skydiver 嗯,现在才意识到
    skydiver
        7
    skydiver  
       2016-08-19 14:05:45 +08:00
    @coderwen 所以也可以用来干坏事儿……
    longaiwp
        8
    longaiwp  
       2016-08-19 15:03:16 +08:00
    @skydiver 也可以干好事
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4058 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 99ms · UTC 00:58 · PVG 08:58 · LAX 17:58 · JFK 20:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.