整数の行列Mが与えられます。 2つの行が同じであるかどうかを確認してください。最適なアプローチを与える。行列内の重複する行をチェックする効率的なアルゴリズム
Example:
[{1, 2, 3},
{3, 4, 5},
{1, 2, 3}]
上記の行列では、行1と行3は同じです。
考えられる解決策:
Given a matrix, we can convert each row in a string (example using to_string()
method of C++ and concatenating each element in a row to a string). We do this
for every row of the matrix, and insert it in a table that is something like
(map<string, int> in C++). And hence, duplicate row can be checked in O(mn) time
for an mxn matrix.
が、私はこれよりも良い行うことができますか?または、上記の方法に欠陥がありますか?
最悪の場合、すべての要素を読み込む必要があるため、私はO(mn)よりも優れているとは思いません。 – Matt
それは@Mattが言った理由のために、最適であろう。ちょっと注意すると、要素を連結するときに区切り文字を置く必要があります。それ以外の場合、 '{1、23}'と '{12,3} 'は同じものとみなされます。 – justhalf
@justhalf:それを指摘してくれてありがとう。 –