유물/알고리즘

<릿코드(파이썬)> 240. Search a 2D Matrix Ⅱ

디벅잉 2022. 4. 1. 21:52
728x90

 

🤖

 

문제

https://leetcode.com/problems/search-a-2d-matrix-ii/

 

Search a 2D Matrix II - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

풀이

이진 탐색으로 풀이를 진행했습니다.

13번째 줄에서 배열의 범위를 초과한다는 오류가 발생하였습니다.

end의 범위를 구할 때 1을 빼주지 않았기 때문입니다.

인덱스의 범위는 0부터 시작하고 마지막 인덱스는 배열의 길이에서 1을 빼야 하는데 아직도 한번씩 실수를 하게 됩니다.

컴퓨터처럼 생각하는 그날까지.. 

아래의 코드는 해당 연산을 수정한 코드입니다.

 

728x90