首页 > 其他 > 详细

find the leftmost column that has number 1

时间:2020-02-07 13:40:32      阅读:62      评论:0      收藏:0      [点我收藏+]

Given a matrix, like this

[[0, 0, 1, 1, 1]
[0, 1, 1, 1, 1]
[0, 0, 1, 1, 1]
[0, 0, 0, 0, 0]]

each cell is either 1 or 0
in each row, from left to right, when you first see a number 1 in the cell, then the rest cells in this row will all be 1
Question: please find the leftmost column that has number 1

解法1:

For each row, do binary search and find the left most column that has number 1.

Time complexity: O(m *  lgn)

解法2:

Start from the top right element, if current value is 1, move left, otherwise, move down.

Time complexity: O(m + n)

find the leftmost column that has number 1

原文:https://www.cnblogs.com/beiyeqingteng/p/12272344.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!