0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LeetCode ::: 74 /// Search a 2D Matrix

Last updated at Posted at 2019-10-17

:snowflake::ear_of_rice: 思考回路

[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
行列は所詮配列の中に配列、べっ…別に怖くなんかないわよ!:(´ºωº`):

  • 時間計算量を O(n^2) にしたくないのでやっぱりポインタにお願い
  • ポインタ ① 号は変数 row、行数を表します。
  • 行を頭からループしますので、ゼロを入れます。
  • ポインタ ② 号は変数 col、列数を表します。
  • 列は、最後尾からループする予定なので、「行の中のアイテム(配列)」の長さマイナス 1 にします。でも、もしこの行列が空っぽだったら、ゼロにします。
  • 行列が空っぽでは無い状態で行数オーバーしないように while ループをします。
  • target 見つかったら true~!
  • お題に書かれてるヒントに注目。
  • 目前のアイテム(matrix[row][col])が target より小さかったら、次の行へ移行。
  • 目前のアイテム(matrix[row][col])が target より**大きかったら、同じ行にあると分かりましたので、前の列へ(列は最後尾からループしたから)**探し続けます。
  • ずっと target を見つからなかったら false です。

See the Pen LeetCode ::: 74 /// Search a 2D Matrix by jujuriri (@jujuriri) on CodePen.

---
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?