LoginSignup
0
0

More than 5 years have passed since last update.

不要輕易生成陣列 - 演算法筆記

Posted at

不要輕易生成陣列,包含

  • Slicing an array
  • Duplicating an array

... 等,會產生另外一個 array instance 的行為

源頭是,在練習做程式的題目的時候,解法雖然寫出來了,
但是因為題目平台給了一個超巨大的陣列,
一有切割、複製陣列等行為,
直接就被判定運算超時。

當碰到巨大資料量的時候,複製陣列或切割陣列等行為,就會對記憶體產生衝擊

對應做法

  • 善用指標標示目前處理範圍或是位置

相對來說,生成型態是 Integer 的指標變數是便宜很多的,
換成指標之後,運算超時的問題也就解決了

修改過後的時間複雜度也有改善。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0