Skip to content

Commit 258d572

Browse files
authored
Merge pull request #745 from fartem/240_Search_a_2D_Matrix_II
2024-10-09 v. 6.7.8: added "240. Search a 2D Matrix II"
2 parents 3a46a2a + 39ffdd7 commit 258d572

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
543543
| 236. Lowest Common Ancestor of a Binary Tree | [Link](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/) | [Link](./lib/medium/236_lowest_common_ancestor_of_a_binary_tree.rb) | [Link](./test/medium/test_236_lowest_common_ancestor_of_a_binary_tree.rb) |
544544
| 237. Delete Node in a Linked List | [Link](https://leetcode.com/problems/delete-node-in-a-linked-list/) | [Link](./lib/medium/237_delete_node_in_a_linked_list.rb) | [Link](./test/medium/test_237_delete_node_in_a_linked_list.rb) |
545545
| 238. Product of Array Except Self | [Link](https://leetcode.com/problems/product-of-array-except-self/) | [Link](./lib/medium/238_product_of_array_except_self.rb) | [Link](./test/medium/test_238_product_of_array_except_self.rb) |
546+
| 240. Search a 2D Matrix II | [Link](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [Link](./lib/medium/240_search_a_2d_matrix_ii.rb) | [Link](./test/medium/test_240_search_a_2d_matrix_ii.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '6.7.7'
8+
s.version = '6.7.8'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/search-a-2d-matrix-ii/
4+
# @param {Integer[][]} matrix
5+
# @param {Integer} target
6+
# @return {Boolean}
7+
def search_matrix240(matrix, target)
8+
matrix.each do |matr|
9+
l = 0
10+
r = matrix.first.length - 1
11+
12+
while l <= r
13+
m = (l + r) / 2
14+
curr = matr[m]
15+
16+
return true if curr == target
17+
18+
if curr > target
19+
r = m - 1
20+
else
21+
l = m + 1
22+
end
23+
end
24+
end
25+
26+
false
27+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/240_search_a_2d_matrix_ii'
5+
require 'minitest/autorun'
6+
7+
class SearchA2DMatrixIITest < ::Minitest::Test
8+
def test_default_one
9+
assert(
10+
search_matrix240(
11+
[
12+
[1, 4, 7, 11, 15],
13+
[2, 5, 8, 12, 19],
14+
[3, 6, 9, 16, 22],
15+
[10, 13, 14, 17, 24],
16+
[18, 21, 23, 26, 30]
17+
],
18+
5
19+
)
20+
)
21+
end
22+
23+
def test_default_two
24+
assert(
25+
!search_matrix240(
26+
[
27+
[1, 4, 7, 11, 15],
28+
[2, 5, 8, 12, 19],
29+
[3, 6, 9, 16, 22],
30+
[10, 13, 14, 17, 24],
31+
[18, 21, 23, 26, 30]
32+
],
33+
20
34+
)
35+
)
36+
end
37+
end

0 commit comments

Comments
 (0)