作者Yuan0124 (YC)
看板C_and_CPP
標題[問題] LeetCode global and static 問題
時間Fri Sep 15 09:13:55 2017
開發平台(Platform): (Ex: Win10, Linux, ...)
MacOS Sierra
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
XCode
問題(Question):
Run code and submit solution 結果不同
餵入的資料(Input):
[-1,0,1,0]
預期的正確結果(Expected Output):
[[-1,0,1]]
錯誤結果(Wrong Output):
[]
程式碼(Code):(請善用置底文網頁, 記得排版)
http://codepad.org/tQbV4UCZ
補充說明(Supplement):
LeetCode 15. 3Sum
放到LeetCode上run code 與 submit solution結果不同,點進問題解說中看到
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.
這段英文有看沒有懂,有大大可以指教嗎?
感謝指教
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 108.189.66.69
※ 文章網址: https://webptt.com/m.aspx?n=bbs/C_and_CPP/M.1505438038.A.ADD.html
1F:推 steve1012: 你貼過去的是那些? 附個 screen shot 吧 09/15 09:23
2F:→ steve1012: 好std sort 不用嗎 09/15 09:23
4F:→ Yuan0124: 小弟想要練習一下快速排序法而已XD 09/15 09:31
5F:推 outofyou: 有看過存取記憶體超過範圍導致這種情形的案例。 09/15 09:47
6F:推 steve1012: 我是指你是直接這段code 全貼嗎 09/15 12:12
7F:→ Yuan0124: 是的,除了main沒貼以外其他都貼了上去 09/16 02:54